Skip to content

Commit 87c31b9

Browse files
Add tests and CSS; fmt
1 parent b33c1b3 commit 87c31b9

File tree

5 files changed

+156
-144
lines changed

5 files changed

+156
-144
lines changed

example/content.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ G_{\mu v} = \frac{8 \pi G}{c^4} T_{\mu v}
7070

7171
We also support math blocks and inline math blocks as well!
7272

73-
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$ and they are
73+
When $a \ne 0$, there are two solutions to $(ax^2 + bx + c = 0)$and they are
7474

7575
$$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
7676

77-
You can even typeset individual letters or whole sentences inline just like $x$
78-
or $Quadratic \; formula$. You can also use math blocks to typeset whole
77+
You can even typeset individual letters or whole sentences inline just like
78+
$x$or $Quadratic \; formula$. You can also use math blocks to typeset whole
7979
equations with $\LaTeX$:
8080

8181
$$ \begin{aligned} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\

mod.ts

+42-37
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { emojify } from "emoji";
2-
import * as Marked from "marked";
32
import GitHubSlugger from "github-slugger";
3+
import he from "he";
4+
import katex from "katex";
5+
import * as Marked from "marked";
46
import markedAlert from "marked-alert";
57
import markedFootnote from "marked-footnote";
68
import { gfmHeadingId } from "marked-gfm-heading-id";
79
import Prism from "prismjs";
810
import sanitizeHtml from "sanitize-html";
9-
import he from "he";
10-
import katex from "katex";
1111

12+
import "https://esm.sh/[email protected]/components/prism-yaml";
1213
import { CSS, KATEX_CLASSES, KATEX_CSS } from "./style.ts";
1314
export { CSS, KATEX_CSS, Marked };
14-
import "https://esm.sh/[email protected]/components/prism-yaml";
1515

1616
Marked.marked.use(markedAlert());
1717
Marked.marked.use(gfmHeadingId());
@@ -39,11 +39,7 @@ export class Renderer extends Marked.Renderer {
3939
this.#slugger = new GitHubSlugger();
4040
}
4141

42-
heading(
43-
text: string,
44-
level: 1 | 2 | 3 | 4 | 5 | 6,
45-
raw: string,
46-
): string {
42+
heading(text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string): string {
4743
const slug = this.#slugger.slug(raw);
4844
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`;
4945
}
@@ -163,10 +159,11 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
163159

164160
const marked_opts = getOpts(opts);
165161

166-
const html =
167-
(opts.inline
162+
const html = (
163+
opts.inline
168164
? Marked.marked.parseInline(markdown, marked_opts)
169-
: Marked.marked.parse(markdown, marked_opts)) as string;
165+
: Marked.marked.parse(markdown, marked_opts)
166+
) as string;
170167

171168
if (opts.disableHtmlSanitization) {
172169
return html;
@@ -241,6 +238,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
241238
"notranslate",
242239
"markdown-alert",
243240
"markdown-alert-*",
241+
"markdown-code-title",
244242
],
245243
span: [
246244
"token",
@@ -322,18 +320,22 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
322320
annotation: ["encoding"], // Only enabled when math is enabled
323321
details: ["open"],
324322
section: ["data-footnotes"],
325-
input: ["checked", "disabled", {
326-
name: "type",
327-
values: ["checkbox"],
328-
}],
323+
input: [
324+
"checked",
325+
"disabled",
326+
{
327+
name: "type",
328+
values: ["checkbox"],
329+
},
330+
],
329331
};
330332

331333
return sanitizeHtml(html, {
332334
transformTags: {
333335
img: transformMedia,
334336
video: transformMedia,
335337
},
336-
allowedTags: [...defaultAllowedTags, ...opts.allowedTags ?? []],
338+
allowedTags: [...defaultAllowedTags, ...(opts.allowedTags ?? [])],
337339
allowedAttributes: mergeAttributes(
338340
defaultAllowedAttributes,
339341
opts.allowedAttributes ?? {},
@@ -366,14 +368,12 @@ function stripTokens(
366368

367369
for (const token of tokens) {
368370
if (token.type === "heading") {
369-
sections[index].header = sections[index].header.trim().replace(
370-
/\n{3,}/g,
371-
"\n",
372-
);
373-
sections[index].content = sections[index].content.trim().replace(
374-
/\n{3,}/g,
375-
"\n",
376-
);
371+
sections[index].header = sections[index].header
372+
.trim()
373+
.replace(/\n{3,}/g, "\n");
374+
sections[index].content = sections[index].content
375+
.trim()
376+
.replace(/\n{3,}/g, "\n");
377377

378378
sections.push({ header: "", depth: token.depth, content: "" });
379379
index += 1;
@@ -498,20 +498,21 @@ export function stripSplitBySections(
498498
markdown: string,
499499
opts: RenderOptions = {},
500500
): MarkdownSections[] {
501-
markdown = emojify(markdown).replace(BLOCK_MATH_REGEXP, "").replace(
502-
INLINE_MATH_REGEXP,
503-
"",
504-
);
501+
markdown = emojify(markdown)
502+
.replace(BLOCK_MATH_REGEXP, "")
503+
.replace(INLINE_MATH_REGEXP, "");
505504
const tokens = Marked.marked.lexer(markdown, {
506505
...getOpts(opts),
507506
tokenizer: new StripTokenizer(),
508507
});
509508

510-
const sections: MarkdownSections[] = [{
511-
header: "",
512-
depth: 0,
513-
content: "",
514-
}];
509+
const sections: MarkdownSections[] = [
510+
{
511+
header: "",
512+
depth: 0,
513+
content: "",
514+
},
515+
];
515516
stripTokens(tokens, sections, false);
516517

517518
return sections;
@@ -521,7 +522,11 @@ export function stripSplitBySections(
521522
* Strip all markdown syntax to get a plaintext output
522523
*/
523524
export function strip(markdown: string, opts: RenderOptions = {}): string {
524-
return stripSplitBySections(markdown, opts).map((section) =>
525-
section.header + "\n\n" + section.content
526-
).join("\n\n").trim().replace(/\n{3,}/g, "\n") + "\n";
525+
return (
526+
stripSplitBySections(markdown, opts)
527+
.map((section) => section.header + "\n\n" + section.content)
528+
.join("\n\n")
529+
.trim()
530+
.replace(/\n{3,}/g, "\n") + "\n"
531+
);
527532
}

style/main.scss

+14-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
&[align="center"] {
2020
margin: 0 auto;
2121
}
22-
22+
2323
display: inline;
2424
}
2525

@@ -41,15 +41,15 @@
4141
padding-top: 2px;
4242
text-align: center;
4343
}
44-
44+
4545
ol {
4646
list-style: decimal;
4747
}
48-
48+
4949
ul {
5050
list-style: disc;
5151
}
52-
52+
5353
table {
5454
width: fit-content;
5555
}
@@ -123,6 +123,16 @@
123123
}
124124
}
125125

126+
.markdown-body .markdown-code-title {
127+
background-color: var(--bgColor-muted, var(--color-canvas-subtle));
128+
padding: 8px 16px;
129+
border-bottom: 1px solid var(--borderColor-muted,var(--color-border-muted));
130+
margin: 0;
131+
font-size: 85%;
132+
font-weight: bold;
133+
display: block;
134+
}
135+
126136
.sr-only {
127137
position: absolute;
128138
width: 1px;

test/fixtures/footnote.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,23 @@ blocks[^bignote].
2222

2323
[^bignote]: The first paragraph of the definition.
2424

25-
Paragraph two of the definition.
25+
Paragraph two of the definition.
2626

27-
> A blockquote with
28-
> multiple lines.
27+
> A blockquote with multiple lines.
2928
30-
~~~
31-
a code block
32-
~~~
29+
```
30+
a code block
31+
```
3332

34-
| Header 1 | Header 2 |
35-
| -------- | -------- |
36-
| Cell 1 | Cell 2 |
33+
| Header 1 | Header 2 |
34+
| -------- | -------- |
35+
| Cell 1 | Cell 2 |
3736

38-
A \`final\` paragraph before list.
37+
A \`final\` paragraph before list.
3938

40-
- Item 1
41-
- Item 2
42-
- Subitem 1
43-
- Subitem 2
39+
- Item 1
40+
- Item 2
41+
- Subitem 1
42+
- Subitem 2
4443

4544
[^@#$%]: A footnote on the label: "@#$%".

0 commit comments

Comments
 (0)