MarkupConverter and HtmlUtils
Convert between Markdown and HTML with MarkupConverter, and extract text from or edit HTML with HtmlUtils, in server scripts.
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.
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
let markdown = MarkupConverter.htmlToMarkdown("<h1>Test</h1><p>Hello!</p>");
Response
# Test #
Hello!
Markdown to HTML
Servicely Script
let html = MarkupConverter.markdownToHTML("# Test # \n Hello!");
Response
<h1>Test</h1>
<p>Hello!</p>
When links are rewritten, the result may come back wrapped in <html> and <body> elements. [UNVERIFIED]
Extract text
Servicely Script
let text = HtmlUtils.extractText("<h1>Test</h1><p>Hello!<br/>World!!</p>");
Response
Test
Hello!
World!!
Change links in HTML
Servicely Script
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
<p><a href="https://example.com">Example</a></p>