prettier-plugin-mdc 0.0.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ray <https://github.com/so1ve>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,45 +1,53 @@
1
1
  # prettier-plugin-mdc
2
2
 
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
3
+ [![NPM version](https://img.shields.io/npm/v/prettier-plugin-mdc?color=a1b858&label=)](https://www.npmjs.com/package/prettier-plugin-mdc)
4
4
 
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
5
+ A [Prettier](https://prettier.io/) plugin for formatting [MDC (Markdown Components)](https://content.nuxt.com/docs/files/markdown#mdc-syntax) syntax used in [Nuxt Content](https://content.nuxt.com/).
6
6
 
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
7
+ ## 💎 Features
8
8
 
9
- ## Purpose
9
+ - Preserve YAML front matter in components
10
+ - Support for nested components
11
+ - Compatible with GFM (GitHub Flavored Markdown) and math syntax
10
12
 
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `prettier-plugin-mdc`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
13
+ ## 📦 Installation
15
14
 
16
- ## What is OIDC Trusted Publishing?
15
+ ```bash
16
+ npm install -D prettier-plugin-mdc
17
+ # or
18
+ yarn add -D prettier-plugin-mdc
19
+ # or
20
+ pnpm add -D prettier-plugin-mdc
21
+ ```
17
22
 
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
23
+ ## 🚀 Usage
19
24
 
20
- ## Setup Instructions
25
+ Add the plugin to your Prettier configuration:
21
26
 
22
- To properly configure OIDC trusted publishing for this package:
27
+ ```json
28
+ // .prettierrc
29
+ {
30
+ "plugins": ["prettier-plugin-mdc"]
31
+ }
32
+ ```
23
33
 
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
34
+ Or in `prettier.config.js`:
28
35
 
29
- ## DO NOT USE THIS PACKAGE
36
+ ```js
37
+ export default {
38
+ plugins: ["prettier-plugin-mdc"],
39
+ };
40
+ ```
30
41
 
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
42
+ Then format your `.mdc` or `.md` files:
36
43
 
37
- ## More Information
44
+ ```bash
45
+ prettier --write "**/*.mdc"
46
+ prettier --write "**/*.md"
47
+ ```
38
48
 
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
49
+ For MDC Syntax Reference, please check [remark-mdc](https://github.com/nuxt-content/remark-mdc).
42
50
 
43
- ---
51
+ ## License
44
52
 
45
- **Maintained for OIDC setup purposes only**
53
+ [MIT](./LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve)
@@ -0,0 +1,13 @@
1
+ import { Parser, Printer } from "prettier";
2
+ import { Node } from "unist";
3
+
4
+ //#region src/constants.d.ts
5
+ declare const AST_FORMAT = "mdc";
6
+ //#endregion
7
+ //#region src/parsers.d.ts
8
+ declare const parsers: Record<typeof AST_FORMAT, Parser<Node>>;
9
+ //#endregion
10
+ //#region src/printers.d.ts
11
+ declare const printers: Record<typeof AST_FORMAT, Printer<Node>>;
12
+ //#endregion
13
+ export { parsers, printers };
package/dist/index.mjs ADDED
@@ -0,0 +1,286 @@
1
+ import { createRequire } from "node:module";
2
+ import markdown from "prettier/parser-markdown";
3
+ import remarkGfm from "remark-gfm";
4
+ import remarkMath from "remark-math";
5
+ import remarkMdc from "remark-mdc";
6
+ import remarkParse from "remark-parse";
7
+ import { unified } from "unified";
8
+ import * as markdown$1 from "prettier/plugins/markdown";
9
+ import { doc } from "prettier";
10
+ import { createSyncFn } from "synckit";
11
+
12
+ //#region src/constants.ts
13
+ const AST_FORMAT = "mdc";
14
+
15
+ //#endregion
16
+ //#region src/parsers.ts
17
+ const parsers = { [AST_FORMAT]: {
18
+ ...markdown.parsers.markdown,
19
+ astFormat: AST_FORMAT,
20
+ parse: async (text) => {
21
+ const processor = unified().use(remarkParse, { commonmark: true }).use(remarkMath).use(remarkGfm).use(remarkMdc);
22
+ return await processor.run(processor.parse(text));
23
+ }
24
+ } };
25
+
26
+ //#endregion
27
+ //#region src/is.ts
28
+ const isTextComponentNode = (node) => node.type === "textComponent";
29
+ const isContainerComponentNode = (node) => node.type === "containerComponent";
30
+ const isComponentContainerSectionNode = (node) => node.type === "componentContainerSection";
31
+ const isLinkNode = (node) => node.type === "link";
32
+ const isWordOrTextNode = (node) => node.type === "word" || node.type === "text";
33
+ /**
34
+ * Check if a node has any textComponent descendants
35
+ */
36
+ function hasTextComponentDescendant(node) {
37
+ if (isTextComponentNode(node)) return true;
38
+ if ("children" in node && Array.isArray(node.children)) return node.children.some(hasTextComponentDescendant);
39
+ return false;
40
+ }
41
+ function hasTextWithBrackets(node) {
42
+ if (isWordOrTextNode(node) && /[[\]]/.test(node.value)) return true;
43
+ if ("children" in node && Array.isArray(node.children)) return node.children.some(hasTextWithBrackets);
44
+ return false;
45
+ }
46
+ /**
47
+ * Check if a link needs custom printing (has textComponent or brackets in text)
48
+ */
49
+ function linkNeedsCustomPrinting(node) {
50
+ if (!node.children) return false;
51
+ for (const child of node.children) if (isTextComponentNode(child) || hasTextWithBrackets(child) || "children" in child && hasTextComponentDescendant(child)) return true;
52
+ return false;
53
+ }
54
+
55
+ //#endregion
56
+ //#region src/visitor-keys.ts
57
+ const visitorKeys = {
58
+ componentContainerSection: ["children"],
59
+ containerComponent: ["children"],
60
+ textComponent: ["children"]
61
+ };
62
+ const mdcNodeTypes = Object.keys(visitorKeys);
63
+
64
+ //#endregion
65
+ //#region src/utils.ts
66
+ const extendedInlineNodes = [
67
+ "image",
68
+ "link",
69
+ "linkReference",
70
+ "strong",
71
+ "inlineCode",
72
+ "emphasis"
73
+ ];
74
+ const extendedInlineNodesHaveAttributes = (node) => extendedInlineNodes.includes(node.type) && "attributes" in node;
75
+ const escapeQuotes = (value, quote) => value.replace(new RegExp(quote, "g"), `\\${quote}`);
76
+
77
+ //#endregion
78
+ //#region src/yaml.ts
79
+ const require = createRequire(import.meta.url);
80
+ const formatYaml = (text, options) => {
81
+ return createSyncFn(require.resolve("./yaml-worker.mjs"))(text, {
82
+ tabWidth: options.tabWidth,
83
+ useTabs: options.useTabs,
84
+ singleQuote: options.singleQuote,
85
+ printWidth: options.printWidth,
86
+ proseWrap: options.proseWrap
87
+ });
88
+ };
89
+
90
+ //#endregion
91
+ //#region src/print.ts
92
+ const { hardline, join } = doc.builders;
93
+ const mapChildren = (path, print) => path.map(print, "children");
94
+ function serializeValue(value, options) {
95
+ const quote = options.singleQuote ? "'" : "\"";
96
+ if (typeof value === "string") return `${quote}${escapeQuotes(value.replace(/\\/g, "\\\\"), quote)}${quote}`;
97
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
98
+ return `${quote}${escapeQuotes(JSON.stringify(value), quote)}${quote}`;
99
+ }
100
+ function printAttributes(node, options) {
101
+ const attrs = node.attributes;
102
+ if (!attrs || Object.keys(attrs).length === 0) return "";
103
+ const parts = [];
104
+ for (const [key, value] of Object.entries(attrs)) if (key === "id") parts.push(`#${value}`);
105
+ else if (key === "class") {
106
+ const classes = String(value).split(/\s+/).filter(Boolean);
107
+ for (const cls of classes) parts.push(`.${cls}`);
108
+ } else if (value === true) parts.push(key);
109
+ else parts.push(`${key}=${serializeValue(value, options)}`);
110
+ return parts.length > 0 ? `{${parts.join(" ")}}` : "";
111
+ }
112
+ /**
113
+ * Print binding component: {{ value }} or {{ value || 'default' }}
114
+ */
115
+ function printBinding(node, options) {
116
+ const value = node.attributes?.value ?? "";
117
+ const defaultValue = node.attributes?.defaultValue;
118
+ const quote = options.singleQuote ? "'" : "\"";
119
+ if (defaultValue !== void 0 && defaultValue !== "undefined") return [`{{ ${value} || ${quote}${escapeQuotes(String(defaultValue), quote)}${quote} }}`];
120
+ return [`{{ ${value} }}`];
121
+ }
122
+ /**
123
+ * Check if span component uses shorthand form [content] vs explicit
124
+ * :span[content]
125
+ */
126
+ function isShorthandSpan(node) {
127
+ if (node.name !== "span") return false;
128
+ const pos = node.position;
129
+ if (!pos) return false;
130
+ const nodeLength = pos.end.offset - pos.start.offset;
131
+ if (node.children && node.children.length > 0) {
132
+ const firstChild = node.children[0];
133
+ if (firstChild.position) return firstChild.position.start.offset === pos.start.offset + 1;
134
+ }
135
+ return nodeLength <= 3;
136
+ }
137
+ /**
138
+ * Print inline text component: :name[content]{attrs} Special cases:
139
+ *
140
+ * - Binding: {{ value }}
141
+ * - Span shorthand without attrs: [content]
142
+ */
143
+ function printTextComponent(path, print, options) {
144
+ const { node } = path;
145
+ if (node.name === "binding") return printBinding(node, options);
146
+ const attrStr = printAttributes(node, options);
147
+ function printChildrenWithEscapedBrackets() {
148
+ const childParts = [];
149
+ for (let i = 0; i < node.children.length; i++) {
150
+ const child = node.children[i];
151
+ if (child.type === "text" || child.type === "sentence" || child.type === "whitespace") childParts.push(escapeTextBrackets(child));
152
+ else childParts.push(path.call(print, "children", i));
153
+ }
154
+ return childParts;
155
+ }
156
+ if (isShorthandSpan(node)) {
157
+ if (node.children && node.children.length > 0) return [
158
+ "[",
159
+ ...printChildrenWithEscapedBrackets(),
160
+ "]",
161
+ attrStr
162
+ ];
163
+ return ["[]", attrStr];
164
+ }
165
+ const parts = [`:${node.name}`];
166
+ if (node.children && node.children.length > 0) parts.push("[", ...printChildrenWithEscapedBrackets(), "]");
167
+ if (attrStr) parts.push(attrStr);
168
+ return parts;
169
+ }
170
+ /**
171
+ * Calculate the nesting depth of a container component
172
+ */
173
+ function getContainerDepth(path) {
174
+ let depth = 0;
175
+ for (const item of path.stack) if (typeof item === "object" && item !== null && "type" in item && item.type === "containerComponent") depth++;
176
+ return Math.max(0, depth - 1);
177
+ }
178
+ /**
179
+ * Print YAML front matter from rawData rawData format: "\nkey: value\n---"
180
+ */
181
+ function printRawData(rawData, options) {
182
+ if (!rawData) return [];
183
+ let content = rawData.slice(1, -3).trimEnd();
184
+ if (!content) return [];
185
+ content = formatYaml(content, options);
186
+ return [
187
+ "---",
188
+ hardline,
189
+ join(hardline, content.split("\n")),
190
+ hardline,
191
+ "---",
192
+ hardline
193
+ ];
194
+ }
195
+ /**
196
+ * Print container component: ::name{attrs}\n---\nfmAttrs\n---\nchildren\n::
197
+ */
198
+ function printContainerComponent(path, print, options) {
199
+ const { node } = path;
200
+ const depth = getContainerDepth(path);
201
+ const colons = ":".repeat(depth + 2);
202
+ const parts = [colons, node.name];
203
+ const attrStr = printAttributes(node, options);
204
+ if (attrStr) parts.push(attrStr);
205
+ parts.push(hardline);
206
+ parts.push(...printRawData(node.rawData, options));
207
+ if (node.children && node.children.length > 0) {
208
+ const childDocs = mapChildren(path, print);
209
+ parts.push(join(hardline, childDocs));
210
+ parts.push(hardline);
211
+ }
212
+ parts.push(colons);
213
+ return parts;
214
+ }
215
+ /**
216
+ * Print component container section (slot): #name\ncontent
217
+ */
218
+ function printComponentContainerSection(path, print) {
219
+ const { node } = path;
220
+ const parts = [];
221
+ if (node.name && node.name !== "default") parts.push(`#${node.name}`, hardline);
222
+ if (node.children && node.children.length > 0) {
223
+ const childDocs = mapChildren(path, print);
224
+ parts.push(join(hardline, childDocs));
225
+ }
226
+ return parts;
227
+ }
228
+ /**
229
+ * Recursively escape brackets in text content
230
+ */
231
+ function escapeTextBrackets(node) {
232
+ if (node.type === "text" || node.type === "word") return node.value.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
233
+ if (node.type === "whitespace") return node.value;
234
+ if ("children" in node && Array.isArray(node.children)) return node.children.map(escapeTextBrackets).join("");
235
+ return "";
236
+ }
237
+ /**
238
+ * Print link with MDC children: [content](url) This is needed because
239
+ * prettier's markdown printer adds breaks between children
240
+ */
241
+ function printLink(path, print, options) {
242
+ const { node } = path;
243
+ const childParts = [];
244
+ for (let i = 0; i < node.children.length; i++) {
245
+ const child = node.children[i];
246
+ if (child.type === "text" || child.type === "sentence" || child.type === "whitespace") childParts.push(escapeTextBrackets(child));
247
+ else childParts.push(path.call(print, "children", i));
248
+ }
249
+ const parts = [
250
+ "[",
251
+ ...childParts,
252
+ "]"
253
+ ];
254
+ parts.push("(", node.url);
255
+ if (node.title) {
256
+ const quote = options.singleQuote ? "'" : "\"";
257
+ parts.push(" ", quote, node.title, quote);
258
+ }
259
+ parts.push(")");
260
+ const attrStr = printAttributes(node, options);
261
+ if (attrStr) parts.push(attrStr);
262
+ return parts;
263
+ }
264
+
265
+ //#endregion
266
+ //#region src/printers.ts
267
+ const mdastPrinter = markdown$1.printers.mdast;
268
+ const printers = { [AST_FORMAT]: {
269
+ ...mdastPrinter,
270
+ getVisitorKeys(node, nonTraversableKeys) {
271
+ if (mdcNodeTypes.includes(node.type)) return visitorKeys[node.type];
272
+ return mdastPrinter.getVisitorKeys(node, nonTraversableKeys);
273
+ },
274
+ print(path, options, print, args) {
275
+ const { node } = path;
276
+ if (isLinkNode(node) && linkNeedsCustomPrinting(node)) return printLink(path, print, options);
277
+ if (extendedInlineNodesHaveAttributes(node)) return [mdastPrinter.print(path, options, print, args), printAttributes(node, options)];
278
+ if (isTextComponentNode(node)) return printTextComponent(path, print, options);
279
+ else if (isContainerComponentNode(node)) return printContainerComponent(path, print, options);
280
+ else if (isComponentContainerSectionNode(node)) return printComponentContainerSection(path, print);
281
+ return mdastPrinter.print(path, options, print, args);
282
+ }
283
+ } };
284
+
285
+ //#endregion
286
+ export { parsers, printers };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,13 @@
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 { };
package/package.json CHANGED
@@ -1,10 +1,78 @@
1
1
  {
2
2
  "name": "prettier-plugin-mdc",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for prettier-plugin-mdc",
3
+ "version": "0.1.1",
4
+ "author": "Ray <i@mk1.io> (@so1ve)",
5
+ "type": "module",
6
+ "description": "Prettier plugin for MDC syntax",
5
7
  "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
10
- }
8
+ "prettier",
9
+ "prettier-plugin",
10
+ "mdc",
11
+ "markdown",
12
+ "remark",
13
+ "unified"
14
+ ],
15
+ "homepage": "https://github.com/so1ve/prettier-plugin-mdc#readme",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/so1ve/prettier-plugin-mdc.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/so1ve/prettier-plugin-mdc/issues"
22
+ },
23
+ "license": "MIT",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": "./dist/index.mjs",
27
+ "./yaml-worker": "./dist/yaml-worker.mjs",
28
+ "./package.json": "./package.json"
29
+ },
30
+ "main": "./dist/index.mjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@types/mdast": "^4.0.4",
41
+ "@types/unist": "^3.0.3",
42
+ "remark-gfm": "^4.0.1",
43
+ "remark-math": "^6.0.0",
44
+ "remark-mdc": "^3.10.0",
45
+ "remark-parse": "^11.0.0",
46
+ "synckit": "^0.11.11",
47
+ "unified": "^11.0.5"
48
+ },
49
+ "devDependencies": {
50
+ "@antfu/ni": "^28.0.0",
51
+ "@so1ve/eslint-config": "^4.1.5",
52
+ "@so1ve/prettier-config": "^4.1.5",
53
+ "@types/node": "^25.0.3",
54
+ "@typescript/native-preview": "7.0.0-dev.20251029.1",
55
+ "bumpp": "^10.3.2",
56
+ "dedent": "^1.7.1",
57
+ "eslint": "^9.39.2",
58
+ "prettier": "^3.7.4",
59
+ "tsdown": "^0.18.3",
60
+ "typescript": "^5.9.3",
61
+ "vite": "^7.3.0",
62
+ "vitest": "^4.0.16"
63
+ },
64
+ "peerDependencies": {
65
+ "prettier": "~3.6.2"
66
+ },
67
+ "scripts": {
68
+ "build": "tsdown",
69
+ "lint": "eslint . && prettier . --check",
70
+ "lint:fix": "eslint . --fix && prettier . --write",
71
+ "release": "bumpp --commit --push --tag",
72
+ "test": "vitest",
73
+ "typecheck": "nr typecheck:isolated && nr typecheck:non-isolated",
74
+ "typecheck:isolated": "tsc --noEmit --project tsconfig.isolated.json",
75
+ "typecheck:non-isolated": "tsgo --noEmit --project tsconfig.non-isolated.json",
76
+ "watch": "tsdown --watch"
77
+ }
78
+ }