Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __test__/text-node-to-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,18 @@ describe('Text Node To HTML', () => {
expect(resultHtml).toEqual(`<strong><em><br />${node.text}</em></strong>`)
done()
})

it('Should return Highlighted string text', done => {
const node = {
...textNode,
highlight: true
}

const resultHtml = textNodeToHTML(node, {
...defaultNodeOption
})

expect(resultHtml).toEqual(`<mark>${textNode.text}</mark>`)
done()
})
})
4 changes: 4 additions & 0 deletions src/helper/enumerate-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <br />
if (!hasMarks && text.includes('\n')) {
Expand Down
4 changes: 2 additions & 2 deletions src/helper/sanitize.ts
Original file line number Diff line number Diff line change
@@ -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 <br /> before processing the HTML tags
input = input.replace(/\n/g, '<br />');
Expand Down
1 change: 1 addition & 0 deletions src/nodes/mark-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum MarkType {

STRIKE_THROUGH = 'strikethrough',
INLINE_CODE = 'inlineCode',
HIGHLIGHT = 'highlight',


SUBSCRIPT = 'subscript',
Expand Down
1 change: 1 addition & 0 deletions src/nodes/text-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class TextNode extends Node {
superscript?: boolean
subscript?: boolean
break?: boolean
highlight?: boolean
classname?: string
id?: string

Expand Down
3 changes: 3 additions & 0 deletions src/options/default-node-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ export const defaultNodeOption: RenderOption = {
[MarkType.INLINE_CODE]:(text: string) => {
return `<span data-type='inlineCode'>${sanitizeHTML(text)}</span>`
},
[MarkType.HIGHLIGHT]:(text: string) => {
return `<mark>${sanitizeHTML(text)}</mark>`
},
[MarkType.SUBSCRIPT]:(text: string) => {
return `<sub>${sanitizeHTML(text)}</sub>`
},
Expand Down