diff --git a/__test__/text-node-to-html.test.ts b/__test__/text-node-to-html.test.ts index 876df5a..c89aba7 100644 --- a/__test__/text-node-to-html.test.ts +++ b/__test__/text-node-to-html.test.ts @@ -212,4 +212,18 @@ describe('Text Node To HTML', () => { expect(resultHtml).toEqual(`
${node.text}
`) done() }) + + it('Should return Highlighted string text', done => { + const node = { + ...textNode, + highlight: true + } + + const resultHtml = textNodeToHTML(node, { + ...defaultNodeOption + }) + + expect(resultHtml).toEqual(`${textNode.text}`) + done() + }) }) \ No newline at end of file diff --git a/src/helper/enumerate-entries.ts b/src/helper/enumerate-entries.ts index 50c790f..df9c532 100644 --- a/src/helper/enumerate-entries.ts +++ b/src/helper/enumerate-entries.ts @@ -85,6 +85,10 @@ export function textNodeToHTML(node: TextNode, renderOption: RenderOption): stri text = (renderOption[MarkType.BOLD] as RenderMark)(text); hasMarks = true; } + if (node.highlight) { + text = (renderOption[MarkType.HIGHLIGHT] as RenderMark)(text); + hasMarks = true; + } // If no marks were applied, but text contains newlines, convert them to
if (!hasMarks && text.includes('\n')) { diff --git a/src/helper/sanitize.ts b/src/helper/sanitize.ts index 40839b1..26125f2 100644 --- a/src/helper/sanitize.ts +++ b/src/helper/sanitize.ts @@ -1,8 +1,8 @@ -type AllowedTags = 'p' | 'a' | 'strong' | 'em' | 'ul' | 'ol' | 'li' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'sub' | 'u' | 'table' | 'thead' | 'tbody' | 'tr' | 'th' | 'td' | 'span' | 'fragment' | 'strike' | 'sup' | 'br'| 'img' | 'colgroup' | 'col' | 'div'; +type AllowedTags = 'p' | 'a' | 'strong' | 'em' | 'ul' | 'ol' | 'li' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'sub' | 'u' | 'table' | 'thead' | 'tbody' | 'tr' | 'th' | 'td' | 'span' | 'fragment' | 'strike' | 'sup' | 'br'| 'img' | 'colgroup' | 'col' | 'div' | 'mark'; type AllowedAttributes = 'href' | 'title' | 'target' | 'alt' | 'src' | 'class' | 'id' | 'style' | 'colspan' | 'rowspan' | 'content-type-uid' | 'data-sys-asset-uid' | 'sys-style-type' | 'data-type' | 'data-width' | 'data-rows' | 'data-cols' | 'data-mtec'; -export function sanitizeHTML(input: string, allowedTags: AllowedTags[] = ['p', 'a', 'strong', 'em', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sub', 'u', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'span', 'fragment', 'sup', 'strike', 'br', 'img', 'colgroup', 'col', 'div'], allowedAttributes: AllowedAttributes[] = ['href', 'title', 'target', 'alt', 'src', 'class', 'id', 'style', 'colspan', 'rowspan', 'content-type-uid', 'data-sys-asset-uid', 'sys-style-type', 'data-type', 'data-width', 'data-rows', 'data-cols', 'data-mtec']): string { +export function sanitizeHTML(input: string, allowedTags: AllowedTags[] = ['p', 'a', 'strong', 'em', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'sub', 'u', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'span', 'fragment', 'sup', 'strike', 'br', 'img', 'colgroup', 'col', 'div', 'mark'], allowedAttributes: AllowedAttributes[] = ['href', 'title', 'target', 'alt', 'src', 'class', 'id', 'style', 'colspan', 'rowspan', 'content-type-uid', 'data-sys-asset-uid', 'sys-style-type', 'data-type', 'data-width', 'data-rows', 'data-cols', 'data-mtec']): string { // Replace newline characters with
before processing the HTML tags input = input.replace(/\n/g, '
'); diff --git a/src/nodes/mark-type.ts b/src/nodes/mark-type.ts index c9bc9d1..0d809db 100644 --- a/src/nodes/mark-type.ts +++ b/src/nodes/mark-type.ts @@ -6,6 +6,7 @@ enum MarkType { STRIKE_THROUGH = 'strikethrough', INLINE_CODE = 'inlineCode', + HIGHLIGHT = 'highlight', SUBSCRIPT = 'subscript', diff --git a/src/nodes/text-node.ts b/src/nodes/text-node.ts index 02f9de0..7bd95f5 100644 --- a/src/nodes/text-node.ts +++ b/src/nodes/text-node.ts @@ -9,6 +9,7 @@ export default class TextNode extends Node { superscript?: boolean subscript?: boolean break?: boolean + highlight?: boolean classname?: string id?: string diff --git a/src/options/default-node-options.ts b/src/options/default-node-options.ts index 2d7d4e8..0339ec4 100644 --- a/src/options/default-node-options.ts +++ b/src/options/default-node-options.ts @@ -251,6 +251,9 @@ export const defaultNodeOption: RenderOption = { [MarkType.INLINE_CODE]:(text: string) => { return `${sanitizeHTML(text)}` }, + [MarkType.HIGHLIGHT]:(text: string) => { + return `${sanitizeHTML(text)}` + }, [MarkType.SUBSCRIPT]:(text: string) => { return `${sanitizeHTML(text)}` },