Skip to content

Commit 4f1f29e

Browse files
authored
fix: more tweaks to improve remark compatibility (#109)
1 parent c893b07 commit 4f1f29e

8 files changed

+60
-29
lines changed

mod.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export { CSS, KATEX_CSS, Marked };
1616
Marked.marked.use(markedAlert());
1717
Marked.marked.use(gfmHeadingId());
1818
Marked.marked.use(markedFootnote());
19+
Marked.marked.use({
20+
walkTokens: (token) => {
21+
// putting a list inside a summary requires a double line break
22+
// but we shouldn't keep that double line break in the output
23+
// this doesn't happen in remark/rehype
24+
if (token.type === "html" && token.text.endsWith("</summary>\n\n")) {
25+
token.text = token.text.replace("</summary>\n\n", "</summary>\n");
26+
}
27+
},
28+
});
1929

2030
export class Renderer extends Marked.Renderer {
2131
allowMath: boolean;
@@ -35,7 +45,7 @@ export class Renderer extends Marked.Renderer {
3545
raw: string,
3646
): string {
3747
const slug = this.#slugger.slug(raw);
38-
return `<h${level} id="${slug}"><a class="anchor" aria-hidden="true" tabindex="-1" href="#${slug}"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>${text}</h${level}>`;
48+
return `<h${level} id="${slug}"><a class="anchor" aria-hidden="true" tabindex="-1" href="#${slug}"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>${text}</h${level}>\n`;
3949
}
4050

4151
image(src: string, title: string | null, alt: string) {
@@ -287,7 +297,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
287297
h5: ["id"],
288298
h6: ["id"],
289299
li: ["id"],
290-
td: ["colspan", "rowspan", "align"],
300+
td: ["colspan", "rowspan", "align", "width"],
291301
iframe: ["src", "width", "height"], // Only used when iframe tags are allowed in the first place.
292302
math: ["xmlns"], // Only enabled when math is enabled
293303
annotation: ["encoding"], // Only enabled when math is enabled

test/fixtures/alerts.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<h1 id="alerts"><a class="anchor" aria-hidden="true" tabindex="-1" href="#alerts"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Alerts</h1><div class="markdown-alert markdown-alert-note">
1+
<h1 id="alerts"><a class="anchor" aria-hidden="true" tabindex="-1" href="#alerts"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Alerts</h1>
2+
<div class="markdown-alert markdown-alert-note">
23
<p class="markdown-alert-title"><svg class="octicon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>Note</p>
34
<p>Highlights information that users should take into account, even when
45
skimming.</p>

test/fixtures/basic.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<h1 id="heading"><a class="anchor" aria-hidden="true" tabindex="-1" href="#heading"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Heading</h1><ul>
1+
<h1 id="heading"><a class="anchor" aria-hidden="true" tabindex="-1" href="#heading"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Heading</h1>
2+
<ul>
23
<li>list1</li>
34
<li>list2</li>
45
<li>list3</li>

test/fixtures/detailsSummaryDel.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<p>Example</p>
2-
<details open>
3-
<summary>Shopping list</summary>
4-
2+
<details open>
3+
<summary>Shopping list</summary>
54
<ul>
65
<li>Vegetables</li>
76
<li>Fruits</li>
87
<li>Fish</li>
98
<li><del>tofu</del></li>
109
</ul>
11-
</details>
10+
</details>

test/fixtures/footnote.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<h1 id="example"><a class="anchor" aria-hidden="true" tabindex="-1" href="#example"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Example</h1><p>Here is a simple footnote<sup><a id="footnote-ref-1" href="#footnote-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup>.
1+
<h1 id="example"><a class="anchor" aria-hidden="true" tabindex="-1" href="#example"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Example</h1>
2+
<p>Here is a simple footnote<sup><a id="footnote-ref-1" href="#footnote-1" data-footnote-ref aria-describedby="footnote-label">1</a></sup>.
23
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
34
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
45
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

test/fixtures/table.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
</thead>
44
<tbody>
55
<tr>
6-
<td align="center" rowspan="2">
7-
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">
6+
<td align="center" width="200px" rowspan="2">
7+
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">
88
<img src="images/51degrees.svg" alt="51degrees" width="75%" height="75%" />
99
</a>
1010
</td>
11-
<td align="left">
12-
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ @51degrees/ua-parser-js</a>
11+
<td align="left" width="400px">
12+
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">↗ @51degrees/ua-parser-js</a>
1313
</td>
1414
</tr>
1515
<tr>
@@ -19,13 +19,13 @@
1919
</p>
2020
<p>This package supports all device types including Apple and Android devices and can be used either in a browser (client-side) or Node.js environment (server-side).
2121
</p>
22-
<p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ 51Degrees <u>UAParser</u> </a> to get started.
22+
<p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">↗ 51Degrees <u>UAParser</u> </a> to get started.
2323
</p>
2424
</td>
2525
</tr>
2626
<tr>
2727
<td colspan="2">
28-
<a href="https://opencollective.com/ua-parser-js">↗ Become a sponsor</a>
28+
<a href="https://opencollective.com/ua-parser-js" rel="noopener noreferrer">↗ Become a sponsor</a>
2929
</td>
3030
</tr>
3131
</tbody>

test/fixtures/table.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<tbody>
55
<tr>
66
<td align="center" width="200px" rowspan="2">
7-
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">
7+
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">
88
<img src="images/51degrees.svg" alt="51degrees" width="75%" height="75%" >
99
</a>
1010
</td>
1111
<td align="left" width="400px">
12-
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ @51degrees/ua-parser-js</a>
12+
<a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">↗ @51degrees/ua-parser-js</a>
1313
</td>
1414
</tr>
1515
<tr>
@@ -19,13 +19,13 @@
1919
</p>
2020
<p>This package supports all device types including Apple and Android devices and can be used either in a browser (client-side) or Node.js environment (server-side).
2121
</p>
22-
<p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ 51Degrees <u>UAParser</u> </a> to get started.
22+
<p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js" rel="noopener noreferrer">↗ 51Degrees <u>UAParser</u> </a> to get started.
2323
</p>
2424
</td>
2525
</tr>
2626
<tr>
2727
<td colspan="2">
28-
<a href="https://opencollective.com/ua-parser-js">↗ Become a sponsor</a>
28+
<a href="https://opencollective.com/ua-parser-js" rel="noopener noreferrer">↗ Become a sponsor</a>
2929
</td>
3030
</tr>
3131
</tbody>

test/test.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Deno.test("render github-slugger not reused", function () {
296296
for (let i = 0; i < 2; i++) {
297297
const html = render("## Hello");
298298
const expected =
299-
`<h2 id="hello"><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Hello</h2>`;
299+
`<h2 id="hello"><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Hello</h2>\n`;
300300
assertEquals(html, expected);
301301
}
302302
});
@@ -338,21 +338,22 @@ Deno.test(
338338
Deno.test("details, summary, and del", () => {
339339
const markdown = `Example
340340
341-
<details open>
342-
<summary>Shopping list</summary>
341+
<details open>
342+
<summary>Shopping list</summary>
343343
344-
* Vegetables
345-
* Fruits
346-
* Fish
347-
* <del>tofu</del>
344+
* Vegetables
345+
* Fruits
346+
* Fish
347+
* <del>tofu</del>
348348
349-
</details>`;
349+
</details>
350+
`;
350351
const expected = Deno.readTextFileSync(
351352
"./test/fixtures/detailsSummaryDel.html",
352353
);
353354

354355
const html = render(markdown);
355-
assertEquals(html, expected.trim());
356+
assertEquals(html, expected);
356357
});
357358

358359
Deno.test("del tag test", () => {
@@ -366,7 +367,7 @@ Deno.test("del tag test", () => {
366367
Deno.test("h1 test", () => {
367368
const markdown = "# Hello";
368369
const result =
369-
`<h1 id="hello"><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Hello</h1>`;
370+
`<h1 id="hello"><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Hello</h1>\n`;
370371

371372
const html = render(markdown);
372373
assertEquals(html, result);
@@ -389,3 +390,21 @@ Deno.test("task list", () => {
389390
const html = render(markdown);
390391
assertEquals(html, expected);
391392
});
393+
394+
Deno.test("anchor test raw", () => {
395+
const markdown =
396+
`<a class="anchor" aria-hidden="true" tabindex="-1" href="#hello">foo</a>`;
397+
const result =
398+
`<p><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello">foo</a></p>\n`;
399+
400+
const html = render(markdown);
401+
assertEquals(html, result);
402+
});
403+
404+
Deno.test("anchor test", () => {
405+
const markdown = `[asdf](#hello)`;
406+
const result = `<p><a href="#hello">asdf</a></p>\n`;
407+
408+
const html = render(markdown);
409+
assertEquals(html, result);
410+
});

0 commit comments

Comments
 (0)