prettier-plugin-mdc 0.1.2 → 0.1.4

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.mjs CHANGED
@@ -1,13 +1,11 @@
1
- import { createRequire } from "node:module";
2
1
  import markdown from "prettier/parser-markdown";
3
2
  import remarkGfm from "remark-gfm";
4
3
  import remarkMath from "remark-math";
5
4
  import remarkMdc from "remark-mdc";
6
5
  import remarkParse from "remark-parse";
7
6
  import { unified } from "unified";
8
- import * as markdown$1 from "prettier/plugins/markdown";
9
7
  import { doc } from "prettier";
10
- import { createSyncFn } from "synckit";
8
+ import * as markdown$1 from "prettier/plugins/markdown";
11
9
 
12
10
  //#region src/constants.ts
13
11
  const AST_FORMAT = "mdc";
@@ -110,30 +108,16 @@ function quoteString(value, options) {
110
108
  const quote = hasPreferred && !hasAlternative ? alternativeQuote : preferredQuote;
111
109
  return `${quote}${escapeQuotes(value.replace(/\\/g, "\\\\"), quote)}${quote}`;
112
110
  }
113
-
114
- //#endregion
115
- //#region src/yaml.ts
116
- const require = createRequire(import.meta.url);
117
- const formatYaml = (text, options) => {
118
- return createSyncFn(require.resolve("./yaml-worker.mjs"))(text, {
119
- tabWidth: options.tabWidth,
120
- useTabs: options.useTabs,
121
- singleQuote: options.singleQuote,
122
- printWidth: options.printWidth,
123
- proseWrap: options.proseWrap
124
- });
125
- };
126
-
127
- //#endregion
128
- //#region src/print.ts
129
- const { hardline, join } = doc.builders;
130
- const mapChildren = (path, print) => path.map(print, "children");
131
111
  function serializeValue(value, options) {
132
112
  if (typeof value === "string") return quoteString(value, options);
133
113
  if (typeof value === "number" || typeof value === "boolean") return String(value);
134
114
  const preferredQuote = options.singleQuote ? "'" : "\"";
135
115
  return `${preferredQuote}${escapeQuotes(JSON.stringify(value), preferredQuote)}${preferredQuote}`;
136
116
  }
117
+
118
+ //#endregion
119
+ //#region src/print.ts
120
+ const { hardline: hardline$1, join } = doc.builders;
137
121
  function printAttributes({ attributes }, options) {
138
122
  if (!attributes || Object.keys(attributes).length === 0) return "";
139
123
  const parts = [];
@@ -215,46 +199,35 @@ function getContainerDepth(path) {
215
199
  for (const item of path.stack) if (typeof item === "object" && item !== null && "type" in item && item.type === "containerComponent") depth++;
216
200
  return Math.max(0, depth - 1);
217
201
  }
218
- /**
219
- * Print YAML front matter from rawData rawData format: "\nkey: value\n---"
220
- */
221
- function printRawData(rawData, options) {
222
- if (!rawData) return [];
223
- let content = rawData.trimEnd().slice(1, -3).trimEnd();
224
- if (!content) return [];
225
- content = formatYaml(content, options);
226
- return [
227
- "---",
228
- hardline,
229
- join(hardline, content.split("\n")),
230
- hardline,
231
- "---",
232
- hardline
233
- ];
234
- }
235
- /**
236
- * Print container component: ::name{attrs}\n---\nfmAttrs\n---\nchildren\n::
237
- */
238
- function printContainerComponent(path, print, options) {
202
+ function printContainerComponentWithYamlDoc(path, print, options, yamlDoc) {
239
203
  const { node } = path;
240
204
  const depth = getContainerDepth(path);
241
205
  const colons = ":".repeat(depth + 2);
242
206
  const parts = [colons, node.name];
243
207
  const attrStr = printAttributes(node, options);
244
208
  if (attrStr) parts.push(attrStr);
245
- parts.push(hardline);
246
- const rawDataDoc = printRawData(node.rawData, options);
247
- parts.push(...rawDataDoc);
209
+ parts.push(hardline$1);
210
+ if (yamlDoc.length > 0) parts.push(...yamlDoc);
248
211
  if (node.children && node.children.length > 0) {
249
- if (rawDataDoc.length > 0 && node.rawData) {
250
- const componentStartLine = node.position?.start.line ?? 0;
212
+ const componentStartLine = node.position?.start.line ?? 0;
213
+ let prevEndLine;
214
+ if (yamlDoc.length > 0 && node.rawData) {
251
215
  const rawDataNewlines = (node.rawData.match(/\n/g) ?? []).length;
252
- const rawDataEndLine = componentStartLine + 1 + rawDataNewlines;
253
- if ((node.children[0].position?.start.line ?? 0) > rawDataEndLine + 1) parts.push(hardline);
216
+ prevEndLine = componentStartLine + 1 + rawDataNewlines;
217
+ } else prevEndLine = componentStartLine;
218
+ const childDocs = [];
219
+ for (let i = 0; i < node.children.length; i++) {
220
+ const child = node.children[i];
221
+ const childStartLine = child.position?.start.line ?? 0;
222
+ if (i > 0) {
223
+ childDocs.push(hardline$1);
224
+ if (childStartLine > prevEndLine + 1) childDocs.push(hardline$1);
225
+ } else if (yamlDoc.length > 0 && node.rawData && childStartLine > prevEndLine + 1) childDocs.push(hardline$1);
226
+ childDocs.push(path.call(print, "children", i));
227
+ prevEndLine = child.position?.end.line ?? prevEndLine;
254
228
  }
255
- const childDocs = mapChildren(path, print);
256
- parts.push(join(hardline, childDocs));
257
- parts.push(hardline);
229
+ parts.push(...childDocs);
230
+ parts.push(hardline$1);
258
231
  }
259
232
  parts.push(colons);
260
233
  return parts;
@@ -265,10 +238,10 @@ function printContainerComponent(path, print, options) {
265
238
  function printComponentContainerSection(path, print) {
266
239
  const { node } = path;
267
240
  const parts = [];
268
- if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline);
241
+ if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline$1);
269
242
  if (node.children && node.children.length > 0) {
270
- const childDocs = mapChildren(path, print);
271
- parts.push(join(hardline, childDocs));
243
+ const childDocs = path.map(print, "children");
244
+ parts.push(join(hardline$1, childDocs));
272
245
  }
273
246
  return parts;
274
247
  }
@@ -311,19 +284,47 @@ function printLink(path, print, options) {
311
284
 
312
285
  //#endregion
313
286
  //#region src/printers.ts
287
+ const { hardline } = doc.builders;
314
288
  const mdastPrinter = markdown$1.printers.mdast;
289
+ function extractYamlContent(rawData) {
290
+ if (!rawData) return;
291
+ return rawData.trimEnd().slice(1, -3).trimEnd() || void 0;
292
+ }
315
293
  const printers = { [AST_FORMAT]: {
316
294
  ...mdastPrinter,
317
295
  getVisitorKeys(node, nonTraversableKeys) {
318
296
  if (mdcNodeTypes.includes(node.type)) return visitorKeys[node.type];
319
297
  return mdastPrinter.getVisitorKeys(node, nonTraversableKeys);
320
298
  },
299
+ embed(path) {
300
+ const { node } = path;
301
+ if (isContainerComponentNode(node) && node.rawData) {
302
+ const yamlContent = extractYamlContent(node.rawData);
303
+ if (yamlContent) return async (textToDoc, print, _path, options) => {
304
+ let yamlDoc;
305
+ try {
306
+ yamlDoc = await textToDoc(yamlContent, { parser: "yaml" });
307
+ } catch {
308
+ yamlDoc = yamlContent;
309
+ }
310
+ return printContainerComponentWithYamlDoc(path, print, options, [
311
+ "---",
312
+ hardline,
313
+ yamlDoc,
314
+ hardline,
315
+ "---",
316
+ hardline
317
+ ]);
318
+ };
319
+ }
320
+ return null;
321
+ },
321
322
  print(path, options, print, args) {
322
323
  const { node } = path;
323
324
  if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
324
325
  if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print, args), printAttributes(node, options)];
325
326
  if (isTextComponentNode(node)) return printTextComponent(path, print, options);
326
- else if (isContainerComponentNode(node)) return printContainerComponent(path, print, options);
327
+ else if (isContainerComponentNode(node)) return printContainerComponentWithYamlDoc(path, print, options, []);
327
328
  else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
328
329
  return mdastPrinter.print(path, options, print, args);
329
330
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-mdc",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "author": "Ray <i@mk1.io> (@so1ve)",
5
5
  "type": "module",
6
6
  "description": "Prettier plugin for MDC syntax",
@@ -24,7 +24,6 @@
24
24
  "sideEffects": false,
25
25
  "exports": {
26
26
  ".": "./dist/index.mjs",
27
- "./yaml-worker": "./dist/yaml-worker.mjs",
28
27
  "./package.json": "./package.json"
29
28
  },
30
29
  "main": "./dist/index.mjs",
@@ -43,7 +42,6 @@
43
42
  "remark-math": "^6.0.0",
44
43
  "remark-mdc": "^3.10.0",
45
44
  "remark-parse": "^11.0.0",
46
- "synckit": "^0.11.11",
47
45
  "unified": "^11.0.5"
48
46
  },
49
47
  "devDependencies": {
@@ -1 +0,0 @@
1
- export { };
@@ -1,13 +0,0 @@
1
- import * as prettier from "prettier";
2
- import { runAsWorker } from "synckit";
3
-
4
- //#region src/yaml-worker.ts
5
- runAsWorker(async (text, options) => {
6
- return (await prettier.format(text, {
7
- ...options,
8
- parser: "yaml"
9
- })).trimEnd();
10
- });
11
-
12
- //#endregion
13
- export { };