prettier-plugin-mdc 0.1.4 → 0.2.1

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.
package/dist/index.d.mts CHANGED
@@ -11,6 +11,6 @@ declare const parsers: Record<typeof AST_FORMAT, Parser<Node>>;
11
11
  declare const printers: Record<typeof AST_FORMAT, Printer<Node>>;
12
12
  //#endregion
13
13
  //#region src/index.d.ts
14
- declare const languages: Partial<SupportLanguage>[];
14
+ declare const languages: SupportLanguage[];
15
15
  //#endregion
16
16
  export { languages, parsers, printers };
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
 
@@ -288,7 +311,7 @@ const { hardline } = doc.builders;
288
311
  const mdastPrinter = markdown$1.printers.mdast;
289
312
  function extractYamlContent(rawData) {
290
313
  if (!rawData) return;
291
- return rawData.trimEnd().slice(1, -3).trimEnd() || void 0;
314
+ return rawData.trimEnd().slice(1).replace(/\s-+$/, "").trimEnd() || void 0;
292
315
  }
293
316
  const printers = { [AST_FORMAT]: {
294
317
  ...mdastPrinter,
@@ -296,18 +319,39 @@ const printers = { [AST_FORMAT]: {
296
319
  if (mdcNodeTypes.includes(node.type)) return visitorKeys[node.type];
297
320
  return mdastPrinter.getVisitorKeys(node, nonTraversableKeys);
298
321
  },
299
- embed(path) {
322
+ embed(path, options) {
300
323
  const { node } = path;
324
+ if (isYamlNode(node)) return async (textToDoc) => {
325
+ let yamlDoc;
326
+ try {
327
+ yamlDoc = await textToDoc(node.value, {
328
+ ...options,
329
+ parser: "yaml"
330
+ });
331
+ } catch {
332
+ yamlDoc = node.value;
333
+ }
334
+ return [
335
+ "---",
336
+ hardline,
337
+ yamlDoc,
338
+ hardline,
339
+ "---"
340
+ ];
341
+ };
301
342
  if (isContainerComponentNode(node) && node.rawData) {
302
343
  const yamlContent = extractYamlContent(node.rawData);
303
- if (yamlContent) return async (textToDoc, print, _path, options) => {
344
+ if (yamlContent) return async (textToDoc, print, _path, options$1) => {
304
345
  let yamlDoc;
305
346
  try {
306
- yamlDoc = await textToDoc(yamlContent, { parser: "yaml" });
347
+ yamlDoc = await textToDoc(yamlContent, {
348
+ ...options$1,
349
+ parser: "yaml"
350
+ });
307
351
  } catch {
308
352
  yamlDoc = yamlContent;
309
353
  }
310
- return printContainerComponentWithYamlDoc(path, print, options, [
354
+ return printContainerComponentWithYamlDoc(path, print, options$1, [
311
355
  "---",
312
356
  hardline,
313
357
  yamlDoc,
@@ -317,16 +361,16 @@ const printers = { [AST_FORMAT]: {
317
361
  ]);
318
362
  };
319
363
  }
320
- return null;
364
+ return mdastPrinter.embed(path, options);
321
365
  },
322
- print(path, options, print, args) {
366
+ print(path, options, print) {
323
367
  const { node } = path;
324
368
  if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
325
- if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print, args), printAttributes(node, options)];
369
+ if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print), printAttributes(node, options)];
326
370
  if (isTextComponentNode(node)) return printTextComponent(path, print, options);
327
371
  else if (isContainerComponentNode(node)) return printContainerComponentWithYamlDoc(path, print, options, []);
328
372
  else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
329
- return mdastPrinter.print(path, options, print, args);
373
+ return mdastPrinter.print(path, options, print);
330
374
  }
331
375
  } };
332
376
 
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.1",
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,13 +57,14 @@
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",
60
64
  "vitest": "^4.0.16"
61
65
  },
62
66
  "peerDependencies": {
63
- "prettier": "~3.6.2"
67
+ "prettier": ">=3.6.0"
64
68
  },
65
69
  "scripts": {
66
70
  "build": "tsdown",