@md-plugins/md-plugin-imports 0.1.0-alpha.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.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024-present, Jeff Galbraith
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,127 @@
1
+ # @md-plugins/md-plugin-imports
2
+
3
+ A **Markdown-It** plugin that extracts and stores `<script import>` blocks from Markdown content. This is useful for managing page-level imports in environments where Markdown is rendered dynamically, such as in Vue.js applications.
4
+
5
+ The imports are stored in the `pageScripts` property of the Markdown-It environment (`env`). During post-processing the array if imports (Set) can be used to inject the required scripts into the Vue SFC.
6
+
7
+ ## Features
8
+
9
+ - Extracts `<script import>` blocks from Markdown content.
10
+ - Stores the extracted imports in the `pageScripts` property of the Markdown-It environment (`env`).
11
+ - Cleans the `<script import>` blocks from the rendered output, leaving the rest of the Markdown intact.
12
+ - Handles multiple `<script import>` blocks gracefully.
13
+
14
+ ## Installation
15
+
16
+ Install the plugin via your preferred package manager:
17
+
18
+ ```bash
19
+ # With npm:
20
+ npm install @md-plugins/md-plugin-imports
21
+ # Or with Yarn:
22
+ yarn add @md-plugins/md-plugin-imports
23
+ # Or with pnpm:
24
+ pnpm add @md-plugins/md-plugin-imports
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Basic Setup
30
+
31
+ ```js
32
+ import MarkdownIt from 'markdown-it';
33
+ import { importsPlugin } from '@md-plugins/md-plugin-imports';
34
+ import type { MarkdownItEnv } from '@md-plugins/shared';
35
+
36
+ const md = new MarkdownIt();
37
+ md.use(importsPlugin);
38
+
39
+ const markdownContent = `
40
+ <script import>
41
+ import A from './A.vue';
42
+ import B from './B.vue';
43
+ </script>
44
+
45
+ # Header
46
+
47
+ Some content here.
48
+ `;
49
+
50
+ const env: MarkdownItEnv = {};
51
+ const renderedOutput = md.render(markdownContent, env);
52
+
53
+ console.log('Rendered Output:', renderedOutput);
54
+ console.log('Page Scripts:', env.pageScripts);
55
+ ```
56
+
57
+ ### Options
58
+
59
+ The `md-plugin-imports` plugin does not currently accept options. All `<script import>` blocks are processed by default.
60
+
61
+ ### Script Block Syntax
62
+
63
+ The plugin recognizes `<script import>` blocks in the following format:
64
+
65
+ ```markdown
66
+ <script import>
67
+ import A from './A.vue';
68
+ import B from './B.vue';
69
+ </script>
70
+ ```
71
+
72
+ ### Notes
73
+
74
+ - Script blocks should start with `<script import>` and end with `</script>`.
75
+ - The plugin trims and parses the contents of the block line-by-line.
76
+
77
+ ## Advanced Usage
78
+
79
+ ### Multiple Script Blocks
80
+
81
+ The plugin supports multiple `<script import>` blocks in a single Markdown file:
82
+
83
+ ```markdown
84
+ <script import>
85
+ import A from './A.vue';
86
+ </script>
87
+
88
+ # Header
89
+
90
+ <script import>
91
+ import B from './B.vue';
92
+ </script>
93
+ ```
94
+
95
+ Both `import A from './A.vue';` and `import B from './B.vue';` will be added to the pageScripts set. Internally, a `Set` is used to ensure uniqueness.
96
+
97
+ ## Frontmatter Compatibility
98
+
99
+ The plugin does not interfere with frontmatter or other Markdown content:
100
+
101
+ ```markdown
102
+ ---
103
+ title: Frontmatter Example
104
+ ---
105
+
106
+ <script import>
107
+ import C from './C.vue';
108
+ </script>
109
+
110
+ # Header
111
+
112
+ Some content here.
113
+ ```
114
+
115
+ The plugin processes the `<script import>` block while leaving the frontmatter intact.
116
+
117
+ ## Testing
118
+
119
+ Run the unit tests with `Vitest`:
120
+
121
+ ```bash
122
+ pnpm test
123
+ ```
124
+
125
+ ## License
126
+
127
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
@@ -0,0 +1,22 @@
1
+ import { PluginSimple } from 'markdown-it';
2
+
3
+ /**
4
+ * A Markdown-It plugin that extracts and stores script imports from the Markdown content.
5
+ *
6
+ * This plugin replaces script import blocks (delimited by `<script import>` and `</script>`) with an empty string,
7
+ * and adds the extracted script content to the `env.pageScripts` set in the Markdown-It environment.
8
+ *
9
+ * @param md - The Markdown-It instance to extend.
10
+ */
11
+ declare const importsPlugin: PluginSimple;
12
+
13
+ declare module '@md-plugins/shared' {
14
+ interface MarkdownItEnv {
15
+ /**
16
+ * An array of page script (import statements) to be included.
17
+ */
18
+ pageScripts?: Set<string>;
19
+ }
20
+ }
21
+
22
+ export { importsPlugin };
@@ -0,0 +1,22 @@
1
+ import { PluginSimple } from 'markdown-it';
2
+
3
+ /**
4
+ * A Markdown-It plugin that extracts and stores script imports from the Markdown content.
5
+ *
6
+ * This plugin replaces script import blocks (delimited by `<script import>` and `</script>`) with an empty string,
7
+ * and adds the extracted script content to the `env.pageScripts` set in the Markdown-It environment.
8
+ *
9
+ * @param md - The Markdown-It instance to extend.
10
+ */
11
+ declare const importsPlugin: PluginSimple;
12
+
13
+ declare module '@md-plugins/shared' {
14
+ interface MarkdownItEnv {
15
+ /**
16
+ * An array of page script (import statements) to be included.
17
+ */
18
+ pageScripts?: Set<string>;
19
+ }
20
+ }
21
+
22
+ export { importsPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ const scriptRE = /^\s*<script import>\n([\s\S]*?)\n\s*<\/script>/gm;
2
+ const importsPlugin = (md) => {
3
+ const render = md.render.bind(md);
4
+ md.render = (src, env = {}) => {
5
+ if (!env.pageScripts) {
6
+ env.pageScripts = /* @__PURE__ */ new Set();
7
+ }
8
+ const mdContent = src.replace(scriptRE, (_, scriptContent) => {
9
+ const imports = scriptContent.split("\n").map((line) => line.trim()).filter(Boolean);
10
+ imports.forEach((importLine) => {
11
+ env.pageScripts.add(importLine);
12
+ });
13
+ return "";
14
+ });
15
+ return render(mdContent, env);
16
+ };
17
+ };
18
+
19
+ export { importsPlugin };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@md-plugins/md-plugin-imports",
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "A markdown-it plugin for handling imports in markdown files.",
5
+ "keywords": [
6
+ "markdown-it",
7
+ "quasarframework",
8
+ "vue",
9
+ "types"
10
+ ],
11
+ "homepage": "https://github.com/md-plugins",
12
+ "bugs": {
13
+ "url": "https://github.com/md-plugins/md-plugins/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/md-plugins/md-plugins.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "hawkeye64 <galbraith64@gmail.com>",
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "import": {
25
+ "types": "./dist/index.d.mts",
26
+ "default": "./dist/index.mjs"
27
+ }
28
+ }
29
+ },
30
+ "module": "./dist/index.mjs",
31
+ "types": "./dist/index.d.ts",
32
+ "files": [
33
+ "./dist"
34
+ ],
35
+ "dependencies": {
36
+ "@types/markdown-it": "^14.1.2",
37
+ "markdown-it": "^14.1.0",
38
+ "@md-plugins/shared": "0.1.0-alpha.1"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "build": "unbuild",
45
+ "clean": "rm -rf dist",
46
+ "test": "vitest"
47
+ }
48
+ }