prettier-plugin-mdc 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +51 -12
  2. package/package.json +10 -6
package/dist/index.mjs CHANGED
@@ -1,9 +1,12 @@
1
+ import { fromMarkdown } from "mdast-util-from-markdown";
2
+ import { frontmatterFromMarkdown } from "mdast-util-frontmatter";
3
+ import { gfmFromMarkdown } from "mdast-util-gfm";
4
+ import { mathFromMarkdown } from "mdast-util-math";
5
+ import { frontmatter } from "micromark-extension-frontmatter";
6
+ import { gfm } from "micromark-extension-gfm";
7
+ import { math } from "micromark-extension-math";
1
8
  import markdown from "prettier/parser-markdown";
2
- import remarkGfm from "remark-gfm";
3
- import remarkMath from "remark-math";
4
- import remarkMdc from "remark-mdc";
5
- import remarkParse from "remark-parse";
6
- import { unified } from "unified";
9
+ import remarkMdc, { micromarkExtension } from "remark-mdc";
7
10
  import { doc } from "prettier";
8
11
  import * as markdown$1 from "prettier/plugins/markdown";
9
12
 
@@ -17,6 +20,7 @@ const isContainerComponentNode = (node) => node.type === "containerComponent";
17
20
  const isComponentContainerSectionNode = (node) => node.type === "componentContainerSection";
18
21
  const isLinkNode = (node) => node.type === "link";
19
22
  const isWordOrTextNode = (node) => node.type === "word" || node.type === "text";
23
+ const isYamlNode = (node) => node.type === "yaml";
20
24
  /**
21
25
  * Check if a node has any textComponent descendants
22
26
  */
@@ -61,12 +65,30 @@ function validateYamlBlocks(ast, text) {
61
65
 
62
66
  //#endregion
63
67
  //#region src/parsers.ts
68
+ function getMdcFromMarkdownExtensions() {
69
+ const data = {};
70
+ remarkMdc.call({ data: () => data }, {});
71
+ return data.fromMarkdownExtensions ?? [];
72
+ }
73
+ const mdcFromMarkdownExtensions = getMdcFromMarkdownExtensions();
64
74
  const parsers = { [AST_FORMAT]: {
65
75
  ...markdown.parsers.markdown,
66
76
  astFormat: AST_FORMAT,
67
- parse: async (text) => {
68
- const processor = unified().use(remarkParse, { commonmark: true }).use(remarkMath).use(remarkGfm).use(remarkMdc);
69
- const ast = await processor.run(processor.parse(text));
77
+ parse(text) {
78
+ const ast = fromMarkdown(text, {
79
+ extensions: [
80
+ frontmatter(["yaml"]),
81
+ gfm(),
82
+ math(),
83
+ micromarkExtension()
84
+ ],
85
+ mdastExtensions: [
86
+ frontmatterFromMarkdown(["yaml"]),
87
+ gfmFromMarkdown(),
88
+ mathFromMarkdown(),
89
+ ...mdcFromMarkdownExtensions
90
+ ]
91
+ });
70
92
  validateYamlBlocks(ast, text);
71
93
  return ast;
72
94
  }
@@ -77,7 +99,8 @@ const parsers = { [AST_FORMAT]: {
77
99
  const visitorKeys = {
78
100
  componentContainerSection: ["children"],
79
101
  containerComponent: ["children"],
80
- textComponent: ["children"]
102
+ textComponent: ["children"],
103
+ yaml: []
81
104
  };
82
105
  const mdcNodeTypes = Object.keys(visitorKeys);
83
106
 
@@ -298,6 +321,22 @@ const printers = { [AST_FORMAT]: {
298
321
  },
299
322
  embed(path) {
300
323
  const { node } = path;
324
+ if (isYamlNode(node)) return async (textToDoc) => {
325
+ let yamlDoc;
326
+ try {
327
+ yamlDoc = await textToDoc(node.value, { parser: "yaml" });
328
+ } catch {
329
+ yamlDoc = node.value;
330
+ }
331
+ return [
332
+ "---",
333
+ hardline,
334
+ yamlDoc,
335
+ hardline,
336
+ "---",
337
+ hardline
338
+ ];
339
+ };
301
340
  if (isContainerComponentNode(node) && node.rawData) {
302
341
  const yamlContent = extractYamlContent(node.rawData);
303
342
  if (yamlContent) return async (textToDoc, print, _path, options) => {
@@ -319,14 +358,14 @@ const printers = { [AST_FORMAT]: {
319
358
  }
320
359
  return null;
321
360
  },
322
- print(path, options, print, args) {
361
+ print(path, options, print) {
323
362
  const { node } = path;
324
363
  if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
325
- if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print, args), printAttributes(node, options)];
364
+ if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print), printAttributes(node, options)];
326
365
  if (isTextComponentNode(node)) return printTextComponent(path, print, options);
327
366
  else if (isContainerComponentNode(node)) return printContainerComponentWithYamlDoc(path, print, options, []);
328
367
  else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
329
- return mdastPrinter.print(path, options, print, args);
368
+ return mdastPrinter.print(path, options, print);
330
369
  }
331
370
  } };
332
371
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-mdc",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "author": "Ray <i@mk1.io> (@so1ve)",
5
5
  "type": "module",
6
6
  "description": "Prettier plugin for MDC syntax",
@@ -38,11 +38,14 @@
38
38
  "dependencies": {
39
39
  "@types/mdast": "^4.0.4",
40
40
  "@types/unist": "^3.0.3",
41
- "remark-gfm": "^4.0.1",
42
- "remark-math": "^6.0.0",
43
- "remark-mdc": "^3.10.0",
44
- "remark-parse": "^11.0.0",
45
- "unified": "^11.0.5"
41
+ "mdast-util-from-markdown": "^2.0.2",
42
+ "mdast-util-frontmatter": "^2.0.1",
43
+ "mdast-util-gfm": "^3.1.0",
44
+ "mdast-util-math": "^3.0.0",
45
+ "micromark-extension-frontmatter": "^2.0.0",
46
+ "micromark-extension-gfm": "^3.0.0",
47
+ "micromark-extension-math": "^3.1.0",
48
+ "remark-mdc": "^3.10.0"
46
49
  },
47
50
  "devDependencies": {
48
51
  "@antfu/ni": "^28.0.0",
@@ -54,6 +57,7 @@
54
57
  "dedent": "^1.7.1",
55
58
  "eslint": "^9.39.2",
56
59
  "prettier": "^3.7.4",
60
+ "prettier-dev": "https://pkg.pr.new/prettier@c54b26c",
57
61
  "tsdown": "^0.18.3",
58
62
  "typescript": "^5.9.3",
59
63
  "vite": "^7.3.0",