prettier-plugin-mdc 0.1.2 → 0.1.3
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 +40 -48
- package/package.json +1 -3
- package/dist/yaml-worker.d.mts +0 -1
- package/dist/yaml-worker.mjs +0 -13
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
|
|
8
|
+
import * as markdown$1 from "prettier/plugins/markdown";
|
|
11
9
|
|
|
12
10
|
//#region src/constants.ts
|
|
13
11
|
const AST_FORMAT = "mdc";
|
|
@@ -111,22 +109,9 @@ function quoteString(value, options) {
|
|
|
111
109
|
return `${quote}${escapeQuotes(value.replace(/\\/g, "\\\\"), quote)}${quote}`;
|
|
112
110
|
}
|
|
113
111
|
|
|
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
112
|
//#endregion
|
|
128
113
|
//#region src/print.ts
|
|
129
|
-
const { hardline, join } = doc.builders;
|
|
114
|
+
const { hardline: hardline$1, join } = doc.builders;
|
|
130
115
|
const mapChildren = (path, print) => path.map(print, "children");
|
|
131
116
|
function serializeValue(value, options) {
|
|
132
117
|
if (typeof value === "string") return quoteString(value, options);
|
|
@@ -215,46 +200,25 @@ function getContainerDepth(path) {
|
|
|
215
200
|
for (const item of path.stack) if (typeof item === "object" && item !== null && "type" in item && item.type === "containerComponent") depth++;
|
|
216
201
|
return Math.max(0, depth - 1);
|
|
217
202
|
}
|
|
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) {
|
|
203
|
+
function printContainerComponentWithYamlDoc(path, print, options, yamlDoc) {
|
|
239
204
|
const { node } = path;
|
|
240
205
|
const depth = getContainerDepth(path);
|
|
241
206
|
const colons = ":".repeat(depth + 2);
|
|
242
207
|
const parts = [colons, node.name];
|
|
243
208
|
const attrStr = printAttributes(node, options);
|
|
244
209
|
if (attrStr) parts.push(attrStr);
|
|
245
|
-
parts.push(hardline);
|
|
246
|
-
|
|
247
|
-
parts.push(...rawDataDoc);
|
|
210
|
+
parts.push(hardline$1);
|
|
211
|
+
if (yamlDoc.length > 0) parts.push(...yamlDoc);
|
|
248
212
|
if (node.children && node.children.length > 0) {
|
|
249
|
-
if (
|
|
213
|
+
if (yamlDoc.length > 0 && node.rawData) {
|
|
250
214
|
const componentStartLine = node.position?.start.line ?? 0;
|
|
251
215
|
const rawDataNewlines = (node.rawData.match(/\n/g) ?? []).length;
|
|
252
216
|
const rawDataEndLine = componentStartLine + 1 + rawDataNewlines;
|
|
253
|
-
if ((node.children[0].position?.start.line ?? 0) > rawDataEndLine + 1) parts.push(hardline);
|
|
217
|
+
if ((node.children[0].position?.start.line ?? 0) > rawDataEndLine + 1) parts.push(hardline$1);
|
|
254
218
|
}
|
|
255
219
|
const childDocs = mapChildren(path, print);
|
|
256
|
-
parts.push(join(hardline, childDocs));
|
|
257
|
-
parts.push(hardline);
|
|
220
|
+
parts.push(join(hardline$1, childDocs));
|
|
221
|
+
parts.push(hardline$1);
|
|
258
222
|
}
|
|
259
223
|
parts.push(colons);
|
|
260
224
|
return parts;
|
|
@@ -265,10 +229,10 @@ function printContainerComponent(path, print, options) {
|
|
|
265
229
|
function printComponentContainerSection(path, print) {
|
|
266
230
|
const { node } = path;
|
|
267
231
|
const parts = [];
|
|
268
|
-
if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline);
|
|
232
|
+
if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline$1);
|
|
269
233
|
if (node.children && node.children.length > 0) {
|
|
270
234
|
const childDocs = mapChildren(path, print);
|
|
271
|
-
parts.push(join(hardline, childDocs));
|
|
235
|
+
parts.push(join(hardline$1, childDocs));
|
|
272
236
|
}
|
|
273
237
|
return parts;
|
|
274
238
|
}
|
|
@@ -311,19 +275,47 @@ function printLink(path, print, options) {
|
|
|
311
275
|
|
|
312
276
|
//#endregion
|
|
313
277
|
//#region src/printers.ts
|
|
278
|
+
const { hardline } = doc.builders;
|
|
314
279
|
const mdastPrinter = markdown$1.printers.mdast;
|
|
280
|
+
function extractYamlContent(rawData) {
|
|
281
|
+
if (!rawData) return;
|
|
282
|
+
return rawData.trimEnd().slice(1, -3).trimEnd() || void 0;
|
|
283
|
+
}
|
|
315
284
|
const printers = { [AST_FORMAT]: {
|
|
316
285
|
...mdastPrinter,
|
|
317
286
|
getVisitorKeys(node, nonTraversableKeys) {
|
|
318
287
|
if (mdcNodeTypes.includes(node.type)) return visitorKeys[node.type];
|
|
319
288
|
return mdastPrinter.getVisitorKeys(node, nonTraversableKeys);
|
|
320
289
|
},
|
|
290
|
+
embed(path) {
|
|
291
|
+
const { node } = path;
|
|
292
|
+
if (isContainerComponentNode(node) && node.rawData) {
|
|
293
|
+
const yamlContent = extractYamlContent(node.rawData);
|
|
294
|
+
if (yamlContent) return async (textToDoc, print, _path, options) => {
|
|
295
|
+
let yamlDoc;
|
|
296
|
+
try {
|
|
297
|
+
yamlDoc = await textToDoc(yamlContent, { parser: "yaml" });
|
|
298
|
+
} catch {
|
|
299
|
+
yamlDoc = yamlContent;
|
|
300
|
+
}
|
|
301
|
+
return printContainerComponentWithYamlDoc(path, print, options, [
|
|
302
|
+
"---",
|
|
303
|
+
hardline,
|
|
304
|
+
yamlDoc,
|
|
305
|
+
hardline,
|
|
306
|
+
"---",
|
|
307
|
+
hardline
|
|
308
|
+
]);
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
},
|
|
321
313
|
print(path, options, print, args) {
|
|
322
314
|
const { node } = path;
|
|
323
315
|
if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
|
|
324
316
|
if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print, args), printAttributes(node, options)];
|
|
325
317
|
if (isTextComponentNode(node)) return printTextComponent(path, print, options);
|
|
326
|
-
else if (isContainerComponentNode(node)) return
|
|
318
|
+
else if (isContainerComponentNode(node)) return printContainerComponentWithYamlDoc(path, print, options, []);
|
|
327
319
|
else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
|
|
328
320
|
return mdastPrinter.print(path, options, print, args);
|
|
329
321
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-mdc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
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": {
|
package/dist/yaml-worker.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/yaml-worker.mjs
DELETED
|
@@ -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 { };
|