Skip to content

Commit bf24d6a

Browse files
authored
chore: update deps and modernize (#143)
1 parent 8bdc049 commit bf24d6a

File tree

10 files changed

+87
-68
lines changed

10 files changed

+87
-68
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ jobs:
3333
run: |
3434
deno task check:types
3535
36-
- name: Install Chromium
37-
run: deno run -A npm:puppeteer browsers install chrome
38-
3936
- name: Run tests
4037
run: |
4138
deno task test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
cov_profile
22
deno.lock
33
.DS_Store
4+
node_modules/

deno.json

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,46 @@
55
"nodeModulesDir": "auto",
66
"imports": {
77
"emoji": "jsr:@denosaurs/emoji@^0.3.1",
8-
"marked": "npm:marked@^12",
8+
"marked": "npm:marked@^17.0.1",
99
"github-slugger": "npm:github-slugger@^2.0",
10-
"marked-alert": "npm:marked-alert@^2.0",
11-
"marked-footnote": "npm:marked-footnote@^1.2",
12-
"marked-gfm-heading-id": "npm:marked-gfm-heading-id@^3.1",
13-
"prismjs": "npm:prismjs@^1.29",
14-
"prismjs-yaml": "npm:prismjs@^1.29/components/prism-yaml.js",
15-
"sanitize-html": "npm:sanitize-html@^2.13",
10+
"marked-alert": "npm:marked-alert@^2.1.2",
11+
"marked-footnote": "npm:marked-footnote@^1.4.0",
12+
"marked-gfm-heading-id": "npm:marked-gfm-heading-id@^4.1.3",
13+
"prismjs": "npm:prismjs@^1.30.0",
14+
"prismjs-yaml": "npm:prismjs@^1.30.0/components/prism-yaml.js",
15+
"prismjs-jsx": "npm:prismjs@^1.30.0/components/prism-jsx.js",
16+
"prismjs-typescript": "npm:prismjs@^1.30.0/components/prism-typescript.js",
17+
"prismjs-tsx": "npm:prismjs@^1.30.0/components/prism-tsx.js",
18+
"prismjs-bash": "npm:prismjs@^1.30.0/components/prism-bash.js",
19+
"prismjs-powershell": "npm:prismjs@^1.30.0/components/prism-powershell.js",
20+
"prismjs-json": "npm:prismjs@^1.30.0/components/prism-json.js",
21+
"prismjs-diff": "npm:prismjs@^1.30.0/components/prism-diff.js",
22+
"@astral/astral": "jsr:@astral/astral@0.5.5",
23+
"deno-dom": "jsr:@b-fuze/deno-dom@^0.1.56",
24+
"sanitize-html": "npm:sanitize-html@^2.17.0",
1625
"he": "npm:he@^1.2",
1726
"katex": "npm:katex@^0.16",
1827
"css": "npm:css@^3.0.0",
19-
"@std/assert": "jsr:@std/assert@^1.0"
28+
"@std/assert": "jsr:@std/assert@^1.0.18"
2029
},
2130
"compilerOptions": {
2231
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"]
2332
},
2433
"tasks": {
2534
"build": "deno run --allow-read --allow-write --allow-net --allow-run --allow-env ./style/patch.ts && deno fmt",
2635
"check:types": "deno check **/*.ts",
27-
"coverage": "rm -rf cov_profile && deno test --allow-read --allow-env --allow-write --allow-run --allow-net --coverage=cov_profile",
36+
"coverage": "rm -rf cov_profile && deno test --allow-sys --allow-read --allow-env --allow-write --allow-run --allow-net --coverage=cov_profile",
2837
"dev": "deno run -A --unstable --watch --no-check ./example/main.ts",
2938
"ok": "deno fmt --check && deno lint && deno task check:types && deno task test",
3039
"report": "deno coverage cov_profile --html",
3140
"server": "deno run -A --watch=test/,mod.ts ./test/runTestServer.ts",
3241
"test": "deno test --allow-sys --allow-read --allow-env --allow-write --allow-run --allow-net"
3342
},
43+
"exclude": [
44+
"./style/node_modules/",
45+
"./style/dist/",
46+
"./style/.parcel-cache/"
47+
],
3448
"fmt": {
3549
"exclude": [
3650
"./test/fixtures/",

example/main.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import { serve } from "https://deno.land/std@0.192.0/http/server.ts";
2-
31
import { CSS, KATEX_CSS, render } from "../mod.ts";
42

5-
import "https://esm.sh/prismjs@1.29.0/components/prism-jsx?no-check&pin=v57";
6-
import "https://esm.sh/prismjs@1.29.0/components/prism-typescript?no-check&pin=v57";
7-
import "https://esm.sh/prismjs@1.29.0/components/prism-tsx?no-check&pin=v57";
8-
import "https://esm.sh/prismjs@1.29.0/components/prism-bash?no-check&pin=v57";
9-
import "https://esm.sh/prismjs@1.29.0/components/prism-powershell?no-check&pin=v57";
10-
import "https://esm.sh/prismjs@1.29.0/components/prism-json?no-check&pin=v57";
11-
import "https://esm.sh/prismjs@1.29.0/components/prism-diff?no-check&pin=v57";
3+
import "prismjs-jsx";
4+
import "prismjs-typescript";
5+
import "prismjs-tsx";
6+
import "prismjs-bash";
7+
import "prismjs-powershell";
8+
import "prismjs-json";
9+
import "prismjs-diff";
1210

1311
const CONTENT_PATH = new URL("./content.md", import.meta.url);
1412

@@ -57,6 +55,4 @@ async function handler(_req: Request): Promise<Response> {
5755
}
5856
}
5957

60-
serve(handler, {
61-
port: 8001,
62-
});
58+
Deno.serve({ port: 8001 }, handler);

mod.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ export class Renderer extends Marked.Renderer {
3939
this.#slugger = new GitHubSlugger();
4040
}
4141

42-
override heading(
43-
text: string,
44-
level: 1 | 2 | 3 | 4 | 5 | 6,
45-
raw: string,
46-
): string {
42+
override heading({
43+
tokens,
44+
depth,
45+
text: raw,
46+
}: Marked.Tokens.Heading): string {
47+
const text = this.parser.parseInline(tokens);
4748
const slug = this.#slugger.slug(raw);
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`;
49+
return `<h${depth} 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${depth}>\n`;
4950
}
5051

51-
override image(src: string, title: string | null, alt: string): string {
52-
return `<img src="${src}" alt="${alt}" title="${title ?? ""}" />`;
52+
override image({ href, title, text }: Marked.Tokens.Image): string {
53+
return `<img src="${href}" alt="${text}" title="${title ?? ""}" />`;
5354
}
5455

55-
override code(code: string, language?: string): string {
56+
override code({ text, lang }: Marked.Tokens.Code): string {
57+
let language = lang;
5658
const isTitleIncluded = language?.match(/\stitle="(.+)"/);
5759
let title = null;
5860
if (isTitleIncluded) {
@@ -67,23 +69,24 @@ export class Renderer extends Marked.Renderer {
6769
// transform math code blocks into HTML+MathML
6870
// https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/
6971
if (language === "math" && this.allowMath) {
70-
return katex.renderToString(code, { displayMode: true });
72+
return katex.renderToString(text, { displayMode: true });
7173
}
7274
const grammar =
7375
language && Object.hasOwnProperty.call(Prism.languages, language)
7476
? Prism.languages[language]
7577
: undefined;
7678
if (grammar === undefined) {
77-
return `<pre><code class="notranslate">${he.encode(code)}</code></pre>`;
79+
return `<pre><code class="notranslate">${he.encode(text)}</code></pre>`;
7880
}
79-
const html = Prism.highlight(code, grammar, language!);
81+
const html = Prism.highlight(text, grammar, language!);
8082
const titleHtml = title
8183
? `<div class="markdown-code-title">${title}</div>`
8284
: ``;
8385
return `<div class="highlight highlight-source-${language} notranslate">${titleHtml}<pre>${html}</pre></div>`;
8486
}
8587

86-
override link(href: string, title: string | null, text: string): string {
88+
override link({ href, title, tokens }: Marked.Tokens.Link): string {
89+
const text = this.parser.parseInline(tokens);
8790
const titleAttr = title ? ` title="${title}"` : "";
8891
if (href.startsWith("#")) {
8992
return `<a href="${href}"${titleAttr}>${text}</a>`;
@@ -131,10 +134,8 @@ function mathify(markdown: string) {
131134

132135
function getOpts(opts: RenderOptions) {
133136
return {
134-
baseUrl: opts.baseUrl,
135137
breaks: opts.breaks ?? false,
136138
gfm: true,
137-
mangle: false,
138139
renderer: opts.renderer ? opts.renderer : new Renderer(opts),
139140
async: false,
140141
};
@@ -383,7 +384,7 @@ function stripTokens(
383384
index += 1;
384385
}
385386

386-
if ("tokens" in token && token.tokens) {
387+
if ("tokens" in token && token.tokens && token.type !== "image") {
387388
stripTokens(token.tokens, sections, token.type === "heading");
388389
}
389390

style.ts

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

style/patch.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import $ from "https://deno.land/x/dax@0.36.0/mod.ts";
1+
// deno-lint-ignore no-import-prefix
2+
import $ from "jsr:@david/dax@^0.45.0";
23
import css from "css";
34

4-
await $`rm -rf style/node_modules/@primer/primitives`;
5+
await $`rm -rf style/node_modules/@primer/primitives style/.parcel-cache style/dist`;
56
await $`npm install`.cwd("./style");
67

78
const colorVariables = new Set<string>();
89
const variableRegex = /--[\w-]+/g;
910

1011
const cwd = $.path("./style");
12+
const markdownScssDir = cwd.join("node_modules/@primer/css/markdown");
1113
const scssFiles = [
1214
cwd.join("main.scss"),
13-
...Array.from(
14-
cwd.expandGlobSync("node_modules/@primer/css/markdown/*.scss"),
15-
).map((e) => e.path),
15+
...Array.from(Deno.readDirSync(markdownScssDir.toString()))
16+
.filter((e) => e.isFile && e.name.endsWith(".scss"))
17+
.map((e) => markdownScssDir.join(e.name)),
1618
];
1719

1820
for (const pathRef of scssFiles) {
@@ -51,7 +53,7 @@ await $`npx parcel build main.scss --no-source-maps`.cwd("./style").quiet();
5153
// KATEX
5254

5355
$.logStep("Fetching katex styles");
54-
const KATEX_BASE_URL = "https://cdn.jsdelivr.net/npm/katex@0.16.9/dist";
56+
const KATEX_BASE_URL = "https://cdn.jsdelivr.net/npm/katex@0.16.28/dist";
5557
let KATEX_CSS = await $.request(`${KATEX_BASE_URL}/katex.min.css`).text();
5658

5759
// Replace url of fonts with a cdn since we aren't packaging these

test/server_test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ Deno.test({
2222
);
2323
return cell ? cell.textContent?.trim() : null;
2424
},
25-
row,
26-
col,
25+
{ args: [row, col] },
2726
);
2827
};
2928

@@ -46,7 +45,7 @@ Deno.test({
4645

4746
const getComputedStyle = (
4847
selector: string,
49-
property: keyof CSSStyleDeclaration,
48+
property: string & keyof CSSStyleDeclaration,
5049
) => {
5150
return page.evaluate(
5251
(selector, property) => {
@@ -57,8 +56,7 @@ Deno.test({
5756
const style = globalThis.getComputedStyle(element);
5857
return style[property];
5958
},
60-
selector,
61-
property,
59+
{ args: [selector, property] },
6260
);
6361
};
6462

@@ -90,17 +88,17 @@ Deno.test(
9088
const scrollPositionBefore = await page.evaluate(() =>
9189
globalThis.scrollY
9290
);
93-
await page.click("#footnote-ref-1"); // click the first footnote link. note that we select by id, not href
91+
await (await page.$("#footnote-ref-1"))!.click(); // click the first footnote link. note that we select by id, not href
9492
const scrollPositionAfter = await page.evaluate(() => globalThis.scrollY);
9593
assert(scrollPositionAfter > scrollPositionBefore);
9694

97-
await page.click("#footnote-ref-bignote");
95+
await (await page.$("#footnote-ref-bignote"))!.click();
9896
const scrollPositionAfter2 = await page.evaluate(() =>
9997
globalThis.scrollY
10098
);
10199
assert(scrollPositionAfter2 === scrollPositionAfter);
102100

103-
await page.click("#footnote-1 > p > a");
101+
await (await page.$("#footnote-1 > p > a"))!.click();
104102
const scrollPositionAfter3 = await page.evaluate(() =>
105103
globalThis.scrollY
106104
);
@@ -152,7 +150,7 @@ Deno.test(
152150
assertEquals(h2Style.border, "");
153151

154152
// 4. Verify blue box around the footnote after clicking
155-
await page.click("#footnote-ref-1");
153+
await (await page.$("#footnote-ref-1"))!.click();
156154
const footnoteStyle = await page.evaluate(() => {
157155
const element = document.querySelector("#footnote-1");
158156
if (element) {

test/test.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { assertEquals, assertStringIncludes } from "@std/assert";
2-
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.43/deno-dom-wasm.ts";
3-
import { render, Renderer, strip, stripSplitBySections } from "../mod.ts";
2+
import { DOMParser } from "deno-dom";
3+
import {
4+
type Marked,
5+
render,
6+
Renderer,
7+
strip,
8+
stripSplitBySections,
9+
} from "../mod.ts";
410

511
Deno.test("Basic markdown", async () => {
612
const markdown = await Deno.readTextFile("./test/fixtures/basic.md");
@@ -93,8 +99,9 @@ Deno.test("custom renderer", () => {
9399
const expected = `<h1 id="custom-renderer">hello world</h1>`;
94100

95101
class CustomRenderer extends Renderer {
96-
override heading(text: string, level: 1 | 2 | 3 | 4 | 5 | 6): string {
97-
return `<h${level} id="custom-renderer">${text}</h${level}>`;
102+
override heading(token: Marked.Tokens.Heading): string {
103+
const text = this.parser.parseInline(token.tokens);
104+
return `<h${token.depth} id="custom-renderer">${text}</h${token.depth}>`;
98105
}
99106
}
100107

@@ -229,9 +236,13 @@ Deno.test("custom allowed classes", async () => {
229236
"./test/fixtures/customAllowedClasses.html",
230237
);
231238
class CustomRenderer extends Renderer {
232-
override list(body: string, ordered: boolean): string {
233-
const type = ordered ? "list-decimal" : "list-disc";
234-
const tag = ordered ? "ol" : "ul";
239+
override list(token: Marked.Tokens.List): string {
240+
let body = "";
241+
for (const item of token.items) {
242+
body += this.listitem(item);
243+
}
244+
const type = token.ordered ? "list-decimal" : "list-disc";
245+
const tag = token.ordered ? "ol" : "ul";
235246
return `<${tag} class="${type}">${body}</${tag}>`;
236247
}
237248
}

test/test_utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { default as puppeteer, type Page } from "npm:puppeteer";
1+
import { launch, type Page } from "@astral/astral";
22
import { CSS, render, type RenderOptions } from "../mod.ts";
33

44
type TestCase = {
@@ -33,14 +33,12 @@ export async function browserTest(
3333
const { serverProcess, address } = await startServer();
3434

3535
try {
36-
const browser = await puppeteer.launch({
36+
const browser = await launch({
3737
args: ["--no-sandbox"],
38-
headless: true,
3938
});
4039

4140
try {
42-
const page = await browser.newPage();
43-
await page.goto(`${address}/${test}`);
41+
const page = await browser.newPage(`${address}/${test}`);
4442
await fn(page);
4543
} finally {
4644
await browser.close();

0 commit comments

Comments
 (0)