@md-plugins/md-plugin-link 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,90 @@
1
+ # @md-plugins/md-plugin-link
2
+
3
+ A **Markdown-It** plugin that customizes the rendering of links in Markdown content. This plugin is especially useful for Vue.js applications, where internal links can be replaced with Vue Router components (e.g., `<router-link>`).
4
+
5
+ ## Features
6
+
7
+ - Replaces standard `<a>` tags with a custom link tag (e.g., `MarkdownLink` or `router-link`) for internal links.
8
+ - Adds a customizable attribute (e.g., `to`) for routing.
9
+ - Automatically injects import statements for required components into the `pageScripts` property in the Markdown-It environment (`env`).
10
+ - Retains external links as standard `<a>` tags.
11
+
12
+ ## Installation
13
+
14
+ Install the plugin via your preferred package manager:
15
+
16
+ ```bash
17
+ # With npm:
18
+ npm install @md-plugins/md-plugin-link
19
+ # Or with Yarn:
20
+ yarn add @md-plugins/md-plugin-link
21
+ # Or with pnpm:
22
+ pnpm add @md-plugins/md-plugin-link
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Setup
28
+
29
+ ```js
30
+ import MarkdownIt from 'markdown-it';
31
+ import { linkPlugin } from '@md-plugins/md-plugin-link';
32
+ import type { MarkdownItEnv } from '@md-plugins/shared';
33
+
34
+ const md = new MarkdownIt();
35
+ md.use(linkPlugin, {
36
+ linkTag: 'MarkdownLink', // Custom link tag (e.g., Vue Router component)
37
+ linkToKeyword: 'to', // Attribute to use for internal links
38
+ pageScript: 'import MarkdownLink from "src/components/md/MarkdownLink.vue";',
39
+ });
40
+
41
+ const markdownContent = `
42
+ [Internal Link](/internal-page)
43
+ [External Link](https://example.com)
44
+ `;
45
+
46
+ const env: MarkdownItEnv = {};
47
+ const renderedOutput = md.render(markdownContent, env);
48
+
49
+ console.log('Rendered Output:', renderedOutput);
50
+ console.log('Page Scripts:', Array.from(env.pageScripts || []));
51
+ ```
52
+
53
+ ### Example Output
54
+
55
+ For the example above, the plugin produces the following output:
56
+
57
+ ```html
58
+ <p>
59
+ <MarkdownLink to="/internal-page">Internal Link</MarkdownLink>
60
+ <a href="https://example.com">External Link</a>
61
+ </p>
62
+ ```
63
+
64
+ Additionally, the `pageScripts` property in the `env` object will contain:
65
+
66
+ ```js
67
+ Set(['import MarkdownLink from "src/components/md/MarkdownLink.vue";']);
68
+ ```
69
+
70
+ ## Options
71
+
72
+ The `md-plugin-link` plugin supports the following options:
73
+
74
+ | Option | Type | Default | Description |
75
+ | ------------- | ------ | ---------------------------------------------------------------- | --------------------------------------------------------------- |
76
+ | linkTag | string | 'MarkdownLink' | Custom tag to use for internal links. |
77
+ | linkToKeyword | string | 'to' | Attribute to use for internal links (e.g., to for router-link). |
78
+ | pageScript | string | 'import MarkdownLink from "src/components/md/MarkdownLink.vue";' | Import statement for required components. |
79
+
80
+ ## Testing
81
+
82
+ Run the tests to ensure the plugin behaves as expected:
83
+
84
+ ```bash
85
+ pnpm test
86
+ ```
87
+
88
+ ## License
89
+
90
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
@@ -0,0 +1,33 @@
1
+ import { PluginWithOptions } from 'markdown-it';
2
+
3
+ interface LinkPluginOptions {
4
+ /**
5
+ * The tag for the internal link
6
+ *
7
+ * @default 'MarkdownLink'
8
+ */
9
+ linkTag?: string;
10
+ /**
11
+ * The "to" keyword" for the internal link
12
+ *
13
+ * @default 'to'
14
+ */
15
+ linkToKeyword?: string;
16
+ /**
17
+ * add import statements to the page.
18
+ *
19
+ */
20
+ pageScript?: string;
21
+ }
22
+ declare module '@md-plugins/shared' {
23
+ interface MarkdownItEnv {
24
+ /**
25
+ * An array of page script (import statements) to be included for tags.
26
+ */
27
+ pageScripts?: Set<string>;
28
+ }
29
+ }
30
+
31
+ declare const linkPlugin: PluginWithOptions<LinkPluginOptions>;
32
+
33
+ export { type LinkPluginOptions, linkPlugin };
@@ -0,0 +1,33 @@
1
+ import { PluginWithOptions } from 'markdown-it';
2
+
3
+ interface LinkPluginOptions {
4
+ /**
5
+ * The tag for the internal link
6
+ *
7
+ * @default 'MarkdownLink'
8
+ */
9
+ linkTag?: string;
10
+ /**
11
+ * The "to" keyword" for the internal link
12
+ *
13
+ * @default 'to'
14
+ */
15
+ linkToKeyword?: string;
16
+ /**
17
+ * add import statements to the page.
18
+ *
19
+ */
20
+ pageScript?: string;
21
+ }
22
+ declare module '@md-plugins/shared' {
23
+ interface MarkdownItEnv {
24
+ /**
25
+ * An array of page script (import statements) to be included for tags.
26
+ */
27
+ pageScripts?: Set<string>;
28
+ }
29
+ }
30
+
31
+ declare const linkPlugin: PluginWithOptions<LinkPluginOptions>;
32
+
33
+ export { type LinkPluginOptions, linkPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,45 @@
1
+ const linkPlugin = (md, {
2
+ linkTag = "MarkdownLink",
3
+ linkToKeyword = "to",
4
+ pageScript = 'import MarkdownLink from "src/components/md/MarkdownLink.vue"'
5
+ } = {}) => {
6
+ md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
7
+ const token = tokens[idx];
8
+ if (!token) {
9
+ return "";
10
+ }
11
+ const hrefIndex = token.attrIndex("href");
12
+ if (hrefIndex < 0 || !token.attrs) {
13
+ return self.renderToken(tokens, idx, options);
14
+ }
15
+ const link = token.attrs[hrefIndex];
16
+ const url = link[1];
17
+ token.tag = linkTag;
18
+ link[0] = linkToKeyword;
19
+ link[1] = decodeURI(url);
20
+ if (pageScript) {
21
+ if (!env.pageScripts) {
22
+ env.pageScripts = /* @__PURE__ */ new Set();
23
+ }
24
+ env.pageScripts.add(pageScript);
25
+ }
26
+ return self.renderToken(tokens, idx, options);
27
+ };
28
+ md.renderer.rules.link_close = (tokens, idx, options, env, self) => {
29
+ const token = tokens[idx];
30
+ if (!token) {
31
+ return "";
32
+ }
33
+ let openIdx = idx - 1;
34
+ while (openIdx >= 0 && tokens[openIdx].type !== "link_open") {
35
+ openIdx--;
36
+ }
37
+ const openingToken = tokens[openIdx];
38
+ if (openingToken && openingToken.tag) {
39
+ token.tag = openingToken.tag;
40
+ }
41
+ return self.renderToken(tokens, idx, options);
42
+ };
43
+ };
44
+
45
+ export { linkPlugin };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@md-plugins/md-plugin-link",
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "A markdown-it plugin for handling links.",
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
+ }