---
title: "MarkupConverter and HtmlUtils"
site: api
version: 2027.1
versions: "all"
url: https://docs-new.servicely.ai/api/2027.1/scripting/integration/markup-converter-and-html-utils/
type: reference
application: platform
last_updated: 2026-12-03
commit: 6777698daac5
---


`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

**Servicely Script**

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

**Response**

```text
# Test #

Hello!
```

### Markdown to HTML

**Servicely Script**

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

**Response**

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

The response above shows the converted content only. Unless `options.anchorTarget` is an empty string, `markdownToHTML` returns a whole document, with that content inside `<html>` and `<body>` elements.

### Extract text

**Servicely Script**

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

**Response**

```text
Test
Hello!
World!!
```

### Change links in HTML

**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();
```

**Response**

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

## Related

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