prettier-plugin-mdc 0.1.3 → 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 +71 -23
  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
 
@@ -108,17 +131,16 @@ function quoteString(value, options) {
108
131
  const quote = hasPreferred && !hasAlternative ? alternativeQuote : preferredQuote;
109
132
  return `${quote}${escapeQuotes(value.replace(/\\/g, "\\\\"), quote)}${quote}`;
110
133
  }
111
-
112
- //#endregion
113
- //#region src/print.ts
114
- const { hardline: hardline$1, join } = doc.builders;
115
- const mapChildren = (path, print) => path.map(print, "children");
116
134
  function serializeValue(value, options) {
117
135
  if (typeof value === "string") return quoteString(value, options);
118
136
  if (typeof value === "number" || typeof value === "boolean") return String(value);
119
137
  const preferredQuote = options.singleQuote ? "'" : "\"";
120
138
  return `${preferredQuote}${escapeQuotes(JSON.stringify(value), preferredQuote)}${preferredQuote}`;
121
139
  }
140
+
141
+ //#endregion
142
+ //#region src/print.ts
143
+ const { hardline: hardline$1, join } = doc.builders;
122
144
  function printAttributes({ attributes }, options) {
123
145
  if (!attributes || Object.keys(attributes).length === 0) return "";
124
146
  const parts = [];
@@ -210,14 +232,24 @@ function printContainerComponentWithYamlDoc(path, print, options, yamlDoc) {
210
232
  parts.push(hardline$1);
211
233
  if (yamlDoc.length > 0) parts.push(...yamlDoc);
212
234
  if (node.children && node.children.length > 0) {
235
+ const componentStartLine = node.position?.start.line ?? 0;
236
+ let prevEndLine;
213
237
  if (yamlDoc.length > 0 && node.rawData) {
214
- const componentStartLine = node.position?.start.line ?? 0;
215
238
  const rawDataNewlines = (node.rawData.match(/\n/g) ?? []).length;
216
- const rawDataEndLine = componentStartLine + 1 + rawDataNewlines;
217
- if ((node.children[0].position?.start.line ?? 0) > rawDataEndLine + 1) parts.push(hardline$1);
239
+ prevEndLine = componentStartLine + 1 + rawDataNewlines;
240
+ } else prevEndLine = componentStartLine;
241
+ const childDocs = [];
242
+ for (let i = 0; i < node.children.length; i++) {
243
+ const child = node.children[i];
244
+ const childStartLine = child.position?.start.line ?? 0;
245
+ if (i > 0) {
246
+ childDocs.push(hardline$1);
247
+ if (childStartLine > prevEndLine + 1) childDocs.push(hardline$1);
248
+ } else if (yamlDoc.length > 0 && node.rawData && childStartLine > prevEndLine + 1) childDocs.push(hardline$1);
249
+ childDocs.push(path.call(print, "children", i));
250
+ prevEndLine = child.position?.end.line ?? prevEndLine;
218
251
  }
219
- const childDocs = mapChildren(path, print);
220
- parts.push(join(hardline$1, childDocs));
252
+ parts.push(...childDocs);
221
253
  parts.push(hardline$1);
222
254
  }
223
255
  parts.push(colons);
@@ -231,7 +263,7 @@ function printComponentContainerSection(path, print) {
231
263
  const parts = [];
232
264
  if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline$1);
233
265
  if (node.children && node.children.length > 0) {
234
- const childDocs = mapChildren(path, print);
266
+ const childDocs = path.map(print, "children");
235
267
  parts.push(join(hardline$1, childDocs));
236
268
  }
237
269
  return parts;
@@ -289,6 +321,22 @@ const printers = { [AST_FORMAT]: {
289
321
  },
290
322
  embed(path) {
291
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
+ };
292
340
  if (isContainerComponentNode(node) && node.rawData) {
293
341
  const yamlContent = extractYamlContent(node.rawData);
294
342
  if (yamlContent) return async (textToDoc, print, _path, options) => {
@@ -310,14 +358,14 @@ const printers = { [AST_FORMAT]: {
310
358
  }
311
359
  return null;
312
360
  },
313
- print(path, options, print, args) {
361
+ print(path, options, print) {
314
362
  const { node } = path;
315
363
  if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
316
- 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)];
317
365
  if (isTextComponentNode(node)) return printTextComponent(path, print, options);
318
366
  else if (isContainerComponentNode(node)) return printContainerComponentWithYamlDoc(path, print, options, []);
319
367
  else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
320
- return mdastPrinter.print(path, options, print, args);
368
+ return mdastPrinter.print(path, options, print);
321
369
  }
322
370
  } };
323
371
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-mdc",
3
- "version": "0.1.3",
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",