Skip to content

Commit 54eba1d

Browse files
authored
chore(markdown): migrate to mdast-util-to-hast (#9941)
* chore(markdown): migrate to mdast-util-to-hast * fixup! chore(markdown): migrate to mdast-util-to-hast * fixup! fixup! chore(markdown): migrate to mdast-util-to-hast * fixup! chore(markdown): migrate to mdast-util-to-hast * fixup! chore(markdown): migrate to mdast-util-to-hast
1 parent 5d52f54 commit 54eba1d

File tree

7 files changed

+75
-140
lines changed

7 files changed

+75
-140
lines changed

Diff for: markdown/m2h/handlers/code.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { Handler, State } from "mdast-util-to-hast";
12
import { u } from "unist-builder";
23

34
/**
45
* Transform a Markdown code block into a <pre>.
56
* Adding the highlight tags as classes prefixed by "brush:"
67
*/
7-
export function code(h, node) {
8+
export function code(state: State, node: any): ReturnType<Handler> {
89
const value = node.value ? node.value + "\n" : "";
910
const lang = node.lang?.replace(/-nolint$/, "");
1011
const meta = (node.meta || "").split(" ");
@@ -26,5 +27,10 @@ export function code(h, node) {
2627
// }
2728
// return h(node.position, "pre", props, [code]);
2829

29-
return h(node.position, "pre", props, [u("text", value)]);
30+
return {
31+
type: "element",
32+
tagName: "pre",
33+
properties: props,
34+
children: [u("text", value)],
35+
};
3036
}

Diff for: markdown/m2h/handlers/dl.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { all, wrap } from "./mdast-util-to-hast-utils.js";
2-
1+
import { Handler } from "mdast-util-to-hast";
32
export const DEFINITION_PREFIX = ": ";
43

54
export function isDefinitionList(node) {
@@ -28,7 +27,7 @@ export function isDefinitionList(node) {
2827
);
2928
}
3029

31-
export function asDefinitionList(h, node) {
30+
export function asDefinitionList(state, node): ReturnType<Handler> {
3231
const children = node.children.flatMap((listItem) => {
3332
const terms = listItem.children.slice(0, -1);
3433
const definition =
@@ -39,25 +38,31 @@ export function asDefinitionList(h, node) {
3938
);
4039

4140
return [
42-
h(
43-
node,
44-
"dt",
45-
{},
46-
all(h, {
41+
{
42+
type: "element",
43+
tagName: "dt",
44+
properties: {},
45+
children: state.all({
4746
...node,
4847
children:
4948
terms.length == 1 && terms[0].type == "paragraph"
5049
? terms[0].children
5150
: terms,
52-
})
53-
),
54-
h(
55-
node,
56-
"dd",
57-
{},
58-
all(h, { ...definition, children: [paragraph, ...rest] })
59-
),
51+
}),
52+
},
53+
{
54+
type: "element",
55+
tagName: "dd",
56+
properties: {},
57+
children: state.all({ ...definition, children: [paragraph, ...rest] }),
58+
},
6059
];
6160
});
62-
return h(node, "dl", {}, wrap(children, true));
61+
62+
return {
63+
type: "element",
64+
tagName: "dl",
65+
properties: {},
66+
children: state.wrap(children, true),
67+
};
6368
}

Diff for: markdown/m2h/handlers/index.ts

+40-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs";
33
import { DEFAULT_LOCALE } from "../../../libs/constants/index.js";
44
import { code } from "./code.js";
55
import { asDefinitionList, isDefinitionList } from "./dl.js";
6-
import { one, all, wrap } from "./mdast-util-to-hast-utils.js";
6+
import { Handler, Handlers, State } from "mdast-util-to-hast";
77

88
/* A utilitary function which parses a JSON gettext file
99
to return a Map with each localized string and its matching ID */
@@ -58,12 +58,12 @@ function getNotecardType(node, locale) {
5858
return type == "warning" || type == "note" || type == "callout" ? type : null;
5959
}
6060

61-
export function buildLocalizedHandlers(locale: string) {
61+
export function buildLocalizedHandlers(locale: string): Handlers {
6262
/* This is only used for the Notecard parsing where the "magit" word depends on the locale */
6363
return {
6464
code,
6565

66-
paragraph(h, node) {
66+
paragraph(state: State, node: any): ReturnType<Handler> {
6767
const [child] = node.children;
6868
// Check for an unnecessarily nested KS-tag and unnest it
6969
if (
@@ -72,13 +72,18 @@ export function buildLocalizedHandlers(locale: string) {
7272
child.value.startsWith("{{") &&
7373
child.value.endsWith("}}")
7474
) {
75-
return one(h, child, node);
75+
return state.one(child, node);
7676
}
7777

78-
return h(node, "p", all(h, node));
78+
return {
79+
type: "element",
80+
tagName: "p",
81+
properties: {},
82+
children: state.all(node),
83+
};
7984
},
8085

81-
blockquote(h, node) {
86+
blockquote(state: State, node: any): ReturnType<Handler> {
8287
const type = getNotecardType(node, locale);
8388
if (type) {
8489
const isCallout = type == "callout";
@@ -89,19 +94,24 @@ export function buildLocalizedHandlers(locale: string) {
8994
node.children[0].children.splice(0, 1);
9095
}
9196
}
92-
return h(
93-
node,
94-
"div",
95-
{ className: isCallout ? [type] : ["notecard", type] },
96-
wrap(all(h, node), true)
97-
);
97+
return {
98+
type: "element",
99+
tagName: "div",
100+
properties: { className: isCallout ? [type] : ["notecard", type] },
101+
children: state.wrap(state.all(node), true),
102+
};
98103
}
99-
return h(node, "blockquote", wrap(all(h, node), true));
104+
return {
105+
type: "element",
106+
tagName: "blockquote",
107+
properties: {},
108+
children: state.wrap(state.all(node), true),
109+
};
100110
},
101111

102-
list(h, node) {
112+
list(state: State, node: any): ReturnType<Handler> {
103113
if (isDefinitionList(node)) {
104-
return asDefinitionList(h, node);
114+
return asDefinitionList(state, node);
105115
}
106116

107117
const name = node.ordered ? "ol" : "ul";
@@ -112,14 +122,24 @@ export function buildLocalizedHandlers(locale: string) {
112122
}
113123

114124
// This removes directly descendent paragraphs
115-
const items = all(h, node).map((item) => ({
125+
const items = state.all(node).map((item) => ({
116126
...item,
117-
children: item.children.flatMap((child) =>
118-
child.tagName == "p" ? child.children : [child]
119-
),
127+
children:
128+
"children" in item
129+
? item.children.flatMap((child) =>
130+
"tagName" in child && child.tagName == "p"
131+
? child.children
132+
: [child]
133+
)
134+
: [],
120135
}));
121136

122-
return h(node, name, props, wrap(items, true));
137+
return {
138+
type: "element",
139+
tagName: name,
140+
properties: props,
141+
children: state.wrap(items, true),
142+
};
123143
},
124144
};
125145
}

Diff for: markdown/m2h/handlers/mdast-util-to-hast-utils.ts

-98
This file was deleted.

Diff for: markdown/m2h/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { unified } from "unified";
22
import parse from "remark-parse";
3-
import remark2rehype from "remark-rehype";
3+
import remarkRehype from "remark-rehype";
44
import stringify from "rehype-stringify";
55
import gfm from "remark-gfm";
66
import raw from "rehype-raw";
@@ -18,7 +18,8 @@ function makeProcessor(options: ProcessorOptions) {
1818
const processor = unified()
1919
.use(parse)
2020
.use(gfm)
21-
.use(remark2rehype, {
21+
// @ts-expect-error Need to investigate why types don't seem to match.
22+
.use(remarkRehype, {
2223
handlers: localizedHandlers,
2324
allowDangerousHtml: true,
2425
})

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"jest-watch-typeahead": "^2.2.2",
207207
"jsdom": "^22.1.0",
208208
"lint-staged": "^13.2.3",
209+
"mdast-util-to-hast": "^13.0.2",
209210
"mini-css-extract-plugin": "^2.7.6",
210211
"node-dev": "^8.0.0",
211212
"peggy": "^3.0.2",

Diff for: yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -10317,7 +10317,7 @@ mdast-util-to-hast@^12.1.0:
1031710317
unist-util-position "^4.0.0"
1031810318
unist-util-visit "^4.0.0"
1031910319

10320-
mdast-util-to-hast@^13.0.0:
10320+
mdast-util-to-hast@^13.0.0, mdast-util-to-hast@^13.0.2:
1032110321
version "13.0.2"
1032210322
resolved "https://registry.yarnpkg.com/mdast-util-to-hast/-/mdast-util-to-hast-13.0.2.tgz#74c0a9f014bb2340cae6118f6fccd75467792be7"
1032310323
integrity sha512-U5I+500EOOw9e3ZrclN3Is3fRpw8c19SMyNZlZ2IS+7vLsNzb2Om11VpIVOR+/0137GhZsFEF6YiKD5+0Hr2Og==

0 commit comments

Comments
 (0)