Skip to content

Commit 75c0ee9

Browse files
authored
feat: add support for Markdown Front Matter parsing (#92)
1 parent 823e1c3 commit 75c0ee9

File tree

6 files changed

+177
-30
lines changed

6 files changed

+177
-30
lines changed

package-lock.json

Lines changed: 108 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@eslint/css": "^0.2.0",
4343
"@eslint/js": "^9.9.0",
4444
"@eslint/json": "^0.4.1",
45-
"@eslint/markdown": "^6.1.1",
45+
"@eslint/markdown": "^6.4.0",
4646
"@lezer/highlight": "^1.2.1",
4747
"@radix-ui/react-accordion": "^1.2.0",
4848
"@radix-ui/react-dialog": "^1.1.1",

src/components/options.tsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useExplorer } from "@/hooks/use-explorer";
1010
import {
1111
jsonModes,
1212
markdownModes,
13+
markdownFrontmatters,
1314
languages,
1415
parsers,
1516
sourceTypes,
@@ -24,6 +25,7 @@ import type {
2425
JsonMode,
2526
Language,
2627
MarkdownMode,
28+
MarkdownFrontmatter,
2729
SourceType,
2830
Version,
2931
} from "@/hooks/use-explorer";
@@ -50,19 +52,36 @@ const JSONPanel: React.FC = () => {
5052
const MarkdownPanel: React.FC = () => {
5153
const explorer = useExplorer();
5254
const { markdownOptions, setMarkdownOptions } = explorer;
53-
const { markdownMode } = markdownOptions;
55+
const { markdownMode, markdownFrontmatter } = markdownOptions;
5456
return (
55-
<LabeledSelect
56-
id="markdownMode"
57-
label="Mode"
58-
value={markdownMode}
59-
onValueChange={(value: string) => {
60-
const markdownMode = value as MarkdownMode;
61-
setMarkdownOptions({ ...markdownOptions, markdownMode });
62-
}}
63-
items={markdownModes}
64-
placeholder="Mode"
65-
/>
57+
<>
58+
<LabeledSelect
59+
id="markdownMode"
60+
label="Mode"
61+
value={markdownMode}
62+
onValueChange={(value: string) => {
63+
const markdownMode = value as MarkdownMode;
64+
setMarkdownOptions({ ...markdownOptions, markdownMode });
65+
}}
66+
items={markdownModes}
67+
placeholder="Mode"
68+
/>
69+
70+
<LabeledSelect
71+
id="markdownFrontmatter"
72+
label="Front Matter"
73+
value={markdownFrontmatter}
74+
onValueChange={(value: string) => {
75+
const markdownFrontmatter = value as MarkdownFrontmatter;
76+
setMarkdownOptions({
77+
...markdownOptions,
78+
markdownFrontmatter,
79+
});
80+
}}
81+
items={markdownFrontmatters}
82+
placeholder="Front Matter"
83+
/>
84+
</>
6685
);
6786
};
6887

src/hooks/use-ast.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,37 @@ export function useAST() {
4343
case "json": {
4444
const { jsonMode } = jsonOptions;
4545
const language = json.languages[jsonMode];
46-
astParseResult = language.parse({ body: code.json });
46+
astParseResult = language.parse({
47+
body: code.json,
48+
path: "",
49+
physicalPath: "",
50+
bom: false,
51+
});
4752
break;
4853
}
4954

5055
case "markdown": {
51-
const { markdownMode } = markdownOptions;
56+
const { markdownMode, markdownFrontmatter } = markdownOptions;
5257
const language = markdown.languages[markdownMode];
53-
astParseResult = language.parse({ body: code.markdown });
58+
astParseResult = language.parse(
59+
{ body: code.markdown, path: "", physicalPath: "", bom: false },
60+
{
61+
languageOptions: {
62+
frontmatter:
63+
markdownFrontmatter === "off"
64+
? false
65+
: markdownFrontmatter,
66+
},
67+
},
68+
);
5469
break;
5570
}
5671

5772
case "css": {
5873
const { cssMode, tolerant } = cssOptions;
5974
const language = css.languages[cssMode];
6075
astParseResult = language.parse(
61-
{ body: code.css },
76+
{ body: code.css, path: "", physicalPath: "", bom: false },
6277
{ languageOptions: { tolerant } },
6378
);
6479
break;

src/hooks/use-explorer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type Version = Exclude<Options["ecmaVersion"], undefined>;
2020
export type Language = "javascript" | "json" | "markdown" | "css";
2121
export type JsonMode = "json" | "jsonc" | "json5";
2222
export type MarkdownMode = "commonmark" | "gfm";
23+
export type MarkdownFrontmatter = "off" | "yaml" | "toml";
2324
export type CssMode = "css";
2425

2526
export type Code = {
@@ -41,6 +42,7 @@ export type JsonOptions = {
4142

4243
export type MarkdownOptions = {
4344
markdownMode: MarkdownMode;
45+
markdownFrontmatter: MarkdownFrontmatter;
4446
};
4547

4648
export type CssOptions = {

src/lib/const.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ export const markdownModes = [
190190
},
191191
];
192192

193+
export const markdownFrontmatters = [
194+
{
195+
value: "off",
196+
label: "Off",
197+
},
198+
{
199+
value: "yaml",
200+
label: "YAML",
201+
},
202+
{
203+
value: "toml",
204+
label: "TOML",
205+
},
206+
];
207+
193208
export const cssModes = [
194209
{
195210
value: "css",
@@ -381,6 +396,7 @@ export const defaultJsonOptions: JsonOptions = {
381396

382397
export const defaultMarkdownOptions: MarkdownOptions = {
383398
markdownMode: "commonmark",
399+
markdownFrontmatter: "off",
384400
};
385401

386402
export const defaultCssOptions: CssOptions = {

0 commit comments

Comments
 (0)