Skip to content

Commit 0aa9d1a

Browse files
committed
Add highlight text transform
1 parent c1a84e6 commit 0aa9d1a

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

__test__/text-node-to-html.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Text Node To HTML', () => {
1515
...textNode,
1616
bold: true
1717
}
18-
18+
1919
const resultHtml = textNodeToHTML(node, {
2020
...defaultNodeOption
2121
})
@@ -212,4 +212,18 @@ describe('Text Node To HTML', () => {
212212
expect(resultHtml).toEqual(`<strong><em><br />${node.text}</em></strong>`)
213213
done()
214214
})
215-
})
215+
216+
it('Should return Highlighted string text', done => {
217+
const node = {
218+
...textNode,
219+
highlight: true
220+
}
221+
222+
const resultHtml = textNodeToHTML(node, {
223+
...defaultNodeOption
224+
})
225+
226+
expect(resultHtml).toEqual(`<mark>${textNode.text}</mark>`)
227+
done()
228+
})
229+
})

src/helper/enumerate-entries.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ export function enumerateContents(
4444

4545
export function textNodeToHTML(node: TextNode, renderOption: RenderOption): string {
4646
let text = replaceHtmlEntities(node.text);
47-
47+
4848
// Convert newlines to <br /> tags if there are no other marks
4949
// This ensures newlines are always handled consistently
5050
let hasMarks = false;
51-
51+
5252
if (node.classname || node.id) {
5353
text = (renderOption[MarkType.CLASSNAME_OR_ID] as RenderMark)(text, node.classname, node.id);
5454
hasMarks = true;
@@ -85,12 +85,16 @@ export function textNodeToHTML(node: TextNode, renderOption: RenderOption): stri
8585
text = (renderOption[MarkType.BOLD] as RenderMark)(text);
8686
hasMarks = true;
8787
}
88-
88+
if (node.highlight) {
89+
text = (renderOption[MarkType.HIGHLIGHT] as RenderMark)(text);
90+
hasMarks = true;
91+
}
92+
8993
// If no marks were applied, but text contains newlines, convert them to <br />
9094
if (!hasMarks && text.includes('\n')) {
9195
text = text.replace(/\n/g, '<br />');
9296
}
93-
97+
9498
return text;
9599
}
96100
export function referenceToHTML(
@@ -121,7 +125,7 @@ export function referenceToHTML(
121125
const aTag = `<a${aTagAttrs}>${entryText}</a>`;
122126
return aTag;
123127
}
124-
128+
125129
if (!renderEmbed && renderOption[node.type] !== undefined) {
126130
return sendToRenderOption(node);
127131
}
@@ -136,7 +140,7 @@ export function referenceToHTML(
136140
if (!item && renderOption[node.type] !== undefined) {
137141
return sendToRenderOption(node);
138142
}
139-
143+
140144
return findRenderString(item, metadata, renderOption);
141145

142146
}
@@ -184,4 +188,4 @@ function nodeToHTML(
184188
return (renderOption.default as RenderNode)(node, next);
185189
}
186190
}
187-
}
191+
}

src/nodes/mark-type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ enum MarkType {
66

77
STRIKE_THROUGH = 'strikethrough',
88
INLINE_CODE = 'inlineCode',
9+
HIGHLIGHT = 'highlight',
910

1011

1112
SUBSCRIPT = 'subscript',
1213
SUPERSCRIPT = 'superscript',
1314
BREAK = 'break'
1415
}
1516

16-
export default MarkType
17+
export default MarkType

src/nodes/text-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class TextNode extends Node {
99
superscript?: boolean
1010
subscript?: boolean
1111
break?: boolean
12+
highlight?: boolean
1213
classname?: string
1314
id?: string
1415

src/options/default-node-options.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function getAttrString(node: Node, key: string): string | undefined {
2424
*/
2525
function buildCommonAttrs(node: Node): string {
2626
if (!node.attrs) return '';
27-
27+
2828
const attrs: string[] = [];
2929
if (node.attrs.style) {
3030
attrs.push(` style="${node.attrs.style}"`);
@@ -134,7 +134,7 @@ export const defaultNodeOption: RenderOption = {
134134
});
135135
colgroupHTML += `</${NodeType.COL_GROUP}>`;
136136
}
137-
137+
138138
// Generate table with colgroup and other attributes
139139
return `<table${buildCommonAttrs(node)}>${colgroupHTML}${sanitizeHTML(next(node.children))}</table>`;
140140
},
@@ -157,7 +157,7 @@ export const defaultNodeOption: RenderOption = {
157157
const colSpan = getAttr(node, 'colSpan');
158158
const rowSpanAttr = rowSpan ? ` rowspan="${rowSpan}"` : '';
159159
const colSpanAttr = colSpan ? ` colspan="${colSpan}"` : '';
160-
160+
161161
return `<th${rowSpanAttr}${colSpanAttr}${buildCommonAttrs(node)}>${sanitizeHTML(next(node.children))}</th>`
162162
},
163163
[NodeType.TABLE_DATA]:(node: Node, next: Next) => {
@@ -167,7 +167,7 @@ export const defaultNodeOption: RenderOption = {
167167
const colSpan = getAttr(node, 'colSpan');
168168
const rowSpanAttr = rowSpan ? ` rowspan="${rowSpan}"` : '';
169169
const colSpanAttr = colSpan ? ` colspan="${colSpan}"` : '';
170-
170+
171171
return `<td${rowSpanAttr}${colSpanAttr}${buildCommonAttrs(node)}>${sanitizeHTML(next(node.children))}</td>`
172172
},
173173
[NodeType.BLOCK_QUOTE]:(node: Node, next: Next) => {
@@ -180,12 +180,12 @@ export const defaultNodeOption: RenderOption = {
180180
['reference']:(node: Node, next: Next) => {
181181
const type = getAttr(node, 'type');
182182
const displayType = getAttr(node, 'display-type');
183-
183+
184184
if ((type === 'entry' || type === 'asset') && displayType === 'link'){
185185
const href = getAttrString(node, 'href') || getAttrString(node, 'url') || '';
186186
const target = getAttrString(node, 'target');
187187
const assetUid = getAttrString(node, 'asset-uid');
188-
188+
189189
let aTagAttrs = buildCommonAttrs(node);
190190
if (href) aTagAttrs += ` href="${href}"`;
191191
if (target) {
@@ -200,7 +200,7 @@ export const defaultNodeOption: RenderOption = {
200200
}
201201
return `<a${aTagAttrs}>${sanitizeHTML(next(node.children))}</a>`;
202202
}
203-
203+
204204
if (type === 'asset') {
205205
const assetLink = getAttrString(node, 'asset-link');
206206
const src = assetLink ? encodeURI(assetLink) : '';
@@ -219,7 +219,7 @@ export const defaultNodeOption: RenderOption = {
219219
const altAttr = alt ? ` alt="${alt}"` : '';
220220
const targetAttr = target === "_blank" ? ` target="_blank"` : '';
221221
const styleAttr = style ? ` style="${style}"` : '';
222-
222+
223223
const imageTag = `<img${assetUidAttr}${classAttr}${srcAttr}${altAttr}${targetAttr}${styleAttr} />`;
224224
const styleAttrFig = style ? ` style="${style}"` : '';
225225

@@ -251,6 +251,9 @@ export const defaultNodeOption: RenderOption = {
251251
[MarkType.INLINE_CODE]:(text: string) => {
252252
return `<span data-type='inlineCode'>${sanitizeHTML(text)}</span>`
253253
},
254+
[MarkType.HIGHLIGHT]:(text: string) => {
255+
return `<mark>${sanitizeHTML(text)}</mark>`
256+
},
254257
[MarkType.SUBSCRIPT]:(text: string) => {
255258
return `<sub>${sanitizeHTML(text)}</sub>`
256259
},

0 commit comments

Comments
 (0)