---
title: "MarkupConverter and HtmlUtils"
site: api
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/api/1.11/scripting/markup-converter-and-html-utils/
type: reference
application: platform
last_updated: 2026-10-19
commit: f1879fd7e955
---


`MarkupConverter` converts Markdown to HTML and back. `HtmlUtils` extracts plain text from HTML and lets a script read and change HTML elements. Both are global objects in server scripts, used most often in integrations that exchange formatted text with other systems.

## Get an instance

Call the methods on the global objects.

```javascript
let html = MarkupConverter.markdownToHTML(markdownText);
let text = HtmlUtils.extractText(html);
```

## MarkupConverter methods

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `htmlToMarkdown(html: string)` | string | Converts HTML to Markdown. Returns null for null. | 1.11 |
| `markdownToHTML(markdown: string)` | string | Converts Markdown to HTML, with tables and strikethrough. Line breaks become `<br />`, and links open in a new window (`target="_blank"`). Returns null for null. | 1.11 |
| `markdownToHTML(markdown: string, options: object)` | string | As above. `options.anchorTarget` sets the `target` of every link. An empty string leaves links unchanged. | 1.11 |

## HtmlUtils methods

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `extractText(html: string)` | string | Removes every tag and decodes entities. A `<p>` starts a new line and a `<br>` ends one. Returns an empty string for null. | 1.11 |
| `extractText(html: string, options: string[])` | string | As above. Include `"escapeJson"` in `options` to escape the text for use inside a JSON string. | 1.11 |
| `parse(html: string)` | HTMLElement | Parses HTML into a document element you can query and change. | 1.11 |

## HTMLElement methods

`HtmlUtils.parse` and `elements()` return HTMLElement objects. `select` returns an HTMLElements list.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `select(cssSelector: string)` | HTMLElements | The elements that match a CSS selector. | 1.11 |
| `forEachSelected(cssSelector: string, fn: function)` | HTMLElement | Calls `fn(element, index)` for each match, and returns the element for chaining. | 1.11 |
| `getAttribute(name: string)` | string | The attribute's value, or an empty string. | 1.11 |
| `setAttribute(name: string, value: string)` | nothing | Sets an attribute. | 1.11 |
| `removeAttribute(name: string)` | nothing | Removes an attribute. | 1.11 |
| `html()` | string | The element's inner HTML. | 1.11 |
| `outerHtml()` | string | The element's HTML including its own tag. | 1.11 |

HTMLElements has these methods.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `elements()` | HTMLElement[] | The matched elements as an array. | 1.11 |
| `html()` | string | The inner HTML of every matched element. | 1.11 |
| `outerHtml()` | string | The HTML of every matched element including their tags. | 1.11 |

## Examples

### HTML to Markdown

<CodeTabs>
  Servicely Script

  ```javascript
  let markdown = MarkupConverter.htmlToMarkdown("<h1>Test</h1><p>Hello!</p>");
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```text
  # Test #

  Hello!
  ```
</CodeTabs>

### Markdown to HTML

<CodeTabs>
  Servicely Script

  ```javascript
  let html = MarkupConverter.markdownToHTML("# Test # \n Hello!");
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```html
  <h1>Test</h1>
  <p>Hello!</p>
  ```
</CodeTabs>

When links are rewritten, the result may come back wrapped in `<html>` and `<body>` elements. [UNVERIFIED]

### Extract text

<CodeTabs>
  Servicely Script

  ```javascript
  let text = HtmlUtils.extractText("<h1>Test</h1><p>Hello!<br/>World!!</p>");
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```text
  Test
  Hello!
  World!!
  ```
</CodeTabs>

### Change links in HTML

<CodeTabs>
  Servicely Script

  ```javascript
  let doc = HtmlUtils.parse('<p><a href="https://example.com" target="_self">Example</a></p>');
  doc.forEachSelected("a", function (link, i) {
      link.removeAttribute("target");
  });
  let result = doc.select("p").outerHtml();
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```html
  <p><a href="https://example.com">Example</a></p>
  ```
</CodeTabs>

## Related

- [HTTP](page:scripting/http)
- [Scripting recipes](page:scripting/recipes)
- [Custom endpoints](page:rest/custom-endpoints)
