Skip to content

Commit 401b26a

Browse files
authored
Merge pull request #5 from mintlify/ronan/revert-v0.1.0
Revert "Merge pull request #4 from mintlify/ronan/bump-next-mdx-remote"
2 parents 4b74aa6 + a20d400 commit 401b26a

File tree

4 files changed

+917
-1037
lines changed

4 files changed

+917
-1037
lines changed

package.json

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mintlify/mdx",
3-
"version": "0.1.0",
3+
"version": "0.0.45",
44
"description": "Markdown parser from Mintlify",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -36,7 +36,6 @@
3636
"@mintlify/eslint-config-typescript": "^1.0.9",
3737
"@mintlify/ts-config": "^2.0.2",
3838
"@tsconfig/recommended": "1.x",
39-
"@types/react": "^18.3.3",
4039
"@typescript-eslint/eslint-plugin": "6.x",
4140
"@typescript-eslint/parser": "6.x",
4241
"eslint": "8.x",
@@ -47,15 +46,13 @@
4746
"typescript": "^5.2.2"
4847
},
4948
"dependencies": {
50-
"@types/hast": "^3.0.4",
51-
"@types/unist": "^3.0.2",
52-
"hast-util-to-string": "^3.0.0",
53-
"next-mdx-remote": "^5.0.0",
54-
"refractor": "^4.8.1",
55-
"rehype-katex": "^7.0.0",
56-
"remark-gfm": "^4.0.0",
57-
"remark-math": "^6.0.0",
58-
"remark-smartypants": "^3.0.1",
59-
"unist-util-visit": "^5.0.0"
49+
"hast-util-to-string": "^2.0.0",
50+
"next-mdx-remote": "^4.4.1",
51+
"refractor": "^4.8.0",
52+
"rehype-katex": "^6.0.3",
53+
"remark-gfm": "^3.0.1",
54+
"remark-math": "^5.1.1",
55+
"remark-smartypants": "^2.0.0",
56+
"unist-util-visit": "^4.1.1"
6057
}
6158
}

src/plugins/rehype/rehypeSyntaxHighlighting.ts

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import { toString } from "hast-util-to-string";
1+
import { Node, toString } from "hast-util-to-string";
22
import { refractor } from "refractor/lib/all.js";
33
import { visit } from "unist-util-visit";
4-
import { Node, Parent } from "unist";
5-
import { Element } from "hast";
6-
7-
const languagePrefix = "language-";
8-
9-
function isElement(node: Node): node is Element {
10-
return node.type === "element";
11-
}
4+
import { Parent } from "unist";
125

136
export const rehypeSyntaxHighlighting = (options: {
147
ignoreMissing?: boolean;
@@ -19,59 +12,70 @@ export const rehypeSyntaxHighlighting = (options: {
1912
}
2013

2114
return (tree: Parent) => {
22-
visit(tree, isElement, (node, _, parent) => {
23-
if (
24-
!parent ||
25-
!isElement(parent) ||
26-
parent.tagName !== "pre" ||
27-
node.tagName !== "code"
28-
) {
29-
return;
30-
}
31-
32-
const lang = getLanguage(node);
15+
visit(
16+
tree,
17+
"element",
18+
(
19+
node: Node & {
20+
tagName: string;
21+
children: Node[];
22+
properties: {
23+
className?: string[];
24+
};
25+
},
26+
_index,
27+
parent?: {
28+
tagName: string;
29+
properties: {
30+
className?: string[];
31+
};
32+
}
33+
) => {
34+
if (!parent || parent.tagName !== "pre" || node.tagName !== "code") {
35+
return;
36+
}
3337

34-
if (lang === null) {
35-
return;
36-
}
38+
const lang = getLanguage(node);
3739

38-
try {
39-
const existingClassName = parent.properties.className ?? [];
40-
if (Array.isArray(existingClassName)) {
41-
parent.properties.className = [
42-
...existingClassName,
43-
languagePrefix + lang,
44-
];
40+
if (lang === null) {
41+
return;
4542
}
46-
const result = refractor.highlight(toString(node), lang);
4743

48-
// @ts-expect-error refractor uses outdated version of @types/hast
49-
node.children = result.children;
50-
} catch (err) {
51-
if (
52-
options.ignoreMissing &&
53-
/Unknown language/.test((err as Error).message)
54-
) {
55-
return;
44+
let result;
45+
try {
46+
parent.properties.className = (
47+
parent.properties.className || []
48+
).concat("language-" + lang);
49+
result = refractor.highlight(toString(node), lang);
50+
node.children = result.children;
51+
} catch (err) {
52+
if (
53+
options.ignoreMissing &&
54+
/Unknown language/.test((err as Error).message)
55+
) {
56+
return;
57+
}
58+
throw err;
5659
}
57-
throw err;
5860
}
59-
});
61+
);
6062
};
6163
};
6264

63-
function getLanguage(node: Element) {
64-
const className = node.properties.className || [];
65-
if (!Array.isArray(className)) {
66-
return null;
65+
function getLanguage(
66+
node: Node & {
67+
tagName: string;
68+
children: Node[];
69+
properties: {
70+
className?: string[];
71+
};
6772
}
73+
) {
74+
const className = node.properties.className || [];
6875

6976
for (const classListItem of className) {
70-
if (
71-
typeof classListItem === "string" &&
72-
classListItem.startsWith(languagePrefix)
73-
) {
74-
return classListItem.slice(languagePrefix.length).toLowerCase();
77+
if (classListItem.slice(0, 9) === "language-") {
78+
return classListItem.slice(9).toLowerCase();
7579
}
7680
}
7781

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
"jsx": "react-jsx",
66
"target": "ES2021",
77
"outDir": "dist",
8-
"declaration": true
8+
"declaration": true,
9+
"moduleResolution": "node",
10+
"module": "esnext",
11+
"allowJs": true
912
},
1013
"include": ["**/*.ts", "**/*.tsx"],
1114
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)