Skip to content

Commit 530f5fc

Browse files
authored
core: make html entities encoding optional in markdownToHtml (#231)
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
1 parent 9fc832d commit 530f5fc

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@notesnook-importer/core",
33
"private": false,
4-
"version": "2.2.2",
4+
"version": "2.2.3",
55
"main": "dist/index.js",
66
"description": "Convert notes from various note taking apps into a simple & standard format.",
77
"scripts": {

packages/core/src/utils/to-html.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ import { Data, Literal, Node, Parent } from "unist";
3636

3737
export function markdowntoHTML(
3838
src: string,
39-
options: { allowDangerousHtml: boolean } = { allowDangerousHtml: true }
39+
options: { allowDangerousHtml: boolean; encodeHtmlEntities?: boolean } = {
40+
allowDangerousHtml: true,
41+
encodeHtmlEntities: true
42+
}
4043
) {
4144
const result = remark()
4245
.use(remarkGfm, { singleTilde: false })
@@ -46,7 +49,7 @@ export function markdowntoHTML(
4649
.use(removeComments)
4750
.use(convertFileEmbeds)
4851
.use(remarkRehype, { allowDangerousHtml: options.allowDangerousHtml })
49-
.use(escapeCode)
52+
.use(escapeCode, { encodeHtmlEntities: options.encodeHtmlEntities })
5053
.use(fixChecklistClasses)
5154
.use(liftLanguageToPreFromCode)
5255
.use(collapseMultilineParagraphs)
@@ -185,31 +188,32 @@ const remarkHighlight: () => Transformer = () => (tree) => {
185188
const children: Node<Data>[] = values.map((str, i) =>
186189
i % 2 === 0
187190
? {
188-
type: "text",
189-
value: str
190-
}
191+
type: "text",
192+
value: str
193+
}
191194
: {
192-
type: "highlight",
193-
data: {
194-
hName: "span",
195-
hProperties: {
196-
style: `background-color: rgb(255, 255, 0);`
197-
}
198-
},
199-
children: [
200-
{
201-
type: "text",
202-
value: str
203-
}
204-
]
205-
}
195+
type: "highlight",
196+
data: {
197+
hName: "span",
198+
hProperties: {
199+
style: `background-color: rgb(255, 255, 0);`
200+
}
201+
},
202+
children: [
203+
{
204+
type: "text",
205+
value: str
206+
}
207+
]
208+
}
206209
);
207210
(parent as Parent).children.splice(index!, 1, ...children);
208211
return ["skip", index];
209212
});
210213
};
211214

212-
const escapeCode: Plugin<[], HastRoot, HastRoot> = function () {
215+
const escapeCode: Plugin<[{ encodeHtmlEntities?: boolean }?], HastRoot, HastRoot> = function (options = {}) {
216+
const { encodeHtmlEntities = true } = options;
213217
return (tree: HastRoot) => {
214218
visit(tree, "element", (node) => {
215219
if (!isElement(node, "code")) return;
@@ -220,7 +224,7 @@ const escapeCode: Plugin<[], HastRoot, HastRoot> = function () {
220224
lines.forEach((line, i) => {
221225
children.push({
222226
type: "text",
223-
value: encodeNonAsciiHTML(line)
227+
value: encodeHtmlEntities ? encodeNonAsciiHTML(line) : line
224228
});
225229
if (i !== lines.length - 1)
226230
children.push({

0 commit comments

Comments
 (0)