Skip to content

Commit a06a614

Browse files
committed
core: fix double encoding of entities
1 parent af36074 commit a06a614

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

packages/core/__tests__/markdown.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function google() {
123123
<co-co>hello</co-co>
124124
\`\`\``)
125125
).toBe(
126-
`<pre class="language-javascript"><code class="language-javascript">function google() {<br/>}<br/><br/>&lt;html&gt;good&lt;/html&gt;<br/>&lt;COmponent&gt;{() =&gt; {}}&lt;/Component&gt;<br/>&lt;co-co&gt;hello&lt;/co-co&gt;<br/></code></pre>`
126+
`<pre class="language-javascript"><code class="language-javascript">function google() {<br/>}<br/><br/>&lt;html>good&lt;/html><br/>&lt;COmponent>{() => {}}&lt;/Component><br/>&lt;co-co>hello&lt;/co-co><br/></code></pre>`
127127
);
128128
});
129129

@@ -175,3 +175,19 @@ test("parse highlight", (t) => {
175175
`<p>I am <span style="background-color: rgb(255, 255, 0);">highlighted</span> <strong>again</strong>.</p>`
176176
);
177177
});
178+
179+
test("do not double encode html entities", async (t) => {
180+
t.expect(
181+
markdowntoHTML(`I am some code with entities:
182+
183+
\`\`\`
184+
I am damn happy to here & and < > what else.
185+
\`\`\`
186+
187+
And **something** else.`)
188+
).toBe(
189+
`<p>I am some code with entities:</p>
190+
<pre><code>I am damn happy to here &#38; and &lt; > what else.<br/></code></pre>
191+
<p>And <strong>something</strong> else.</p>`
192+
);
193+
});

packages/core/patches/hast-util-to-html+8.0.4.patch

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ export function markdowntoHTML(
4949
.use(removeComments)
5050
.use(convertFileEmbeds)
5151
.use(remarkRehype, { allowDangerousHtml: options.allowDangerousHtml })
52-
.use(escapeCode, { encodeHtmlEntities: options.encodeHtmlEntities })
52+
.use(escapeCode)
5353
.use(fixChecklistClasses)
5454
.use(liftLanguageToPreFromCode)
5555
.use(collapseMultilineParagraphs)
5656
.use(rehypeStringify, {
5757
allowDangerousHtml: options.allowDangerousHtml,
5858
tightSelfClosing: true,
5959
closeSelfClosing: true,
60-
closeEmptyElements: true
60+
closeEmptyElements: true,
61+
characterReferences: {
62+
useShortestReferences: true
63+
}
6164
})
6265
.processSync(src);
6366
return result.value as string;
@@ -188,32 +191,31 @@ const remarkHighlight: () => Transformer = () => (tree) => {
188191
const children: Node<Data>[] = values.map((str, i) =>
189192
i % 2 === 0
190193
? {
191-
type: "text",
192-
value: str
193-
}
194+
type: "text",
195+
value: str
196+
}
194197
: {
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-
}
198+
type: "highlight",
199+
data: {
200+
hName: "span",
201+
hProperties: {
202+
style: `background-color: rgb(255, 255, 0);`
203+
}
204+
},
205+
children: [
206+
{
207+
type: "text",
208+
value: str
209+
}
210+
]
211+
}
209212
);
210213
(parent as Parent).children.splice(index!, 1, ...children);
211214
return ["skip", index];
212215
});
213216
};
214217

215-
const escapeCode: Plugin<[{ encodeHtmlEntities?: boolean }?], HastRoot, HastRoot> = function (options = {}) {
216-
const { encodeHtmlEntities = true } = options;
218+
const escapeCode: Plugin<[], HastRoot, HastRoot> = function () {
217219
return (tree: HastRoot) => {
218220
visit(tree, "element", (node) => {
219221
if (!isElement(node, "code")) return;
@@ -224,7 +226,7 @@ const escapeCode: Plugin<[{ encodeHtmlEntities?: boolean }?], HastRoot, HastRoot
224226
lines.forEach((line, i) => {
225227
children.push({
226228
type: "text",
227-
value: encodeHtmlEntities ? encodeNonAsciiHTML(line) : line
229+
value: line
228230
});
229231
if (i !== lines.length - 1)
230232
children.push({

0 commit comments

Comments
 (0)