@md-plugins/md-plugin-title 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,85 @@
1
+ # @md-plugins/md-plugin-title
2
+
3
+ A **Markdown-It** plugin that extracts the first `<h1>` title from Markdown content and stores it in the Markdown-It environment (`env`). This is particularly useful for generating page titles dynamically or for metadata extraction in documentation and content management systems.
4
+
5
+ ## Features
6
+
7
+ - Extracts the first `<h1>` from Markdown content.
8
+ - Stores the extracted title in the `title` property of the Markdown-It environment (`env`).
9
+ - Provides flexibility to handle scenarios with or without a title.
10
+ - Seamlessly integrates into content pipelines for title-based features.
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-title
19
+ # Or with Yarn:
20
+ yarn add @md-plugins/md-plugin-title
21
+ # Or with pnpm:
22
+ pnpm add @md-plugins/md-plugin-title
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Setup
28
+
29
+ ```js
30
+ import MarkdownIt from 'markdown-it';
31
+ import { titlePlugin } from '@md-plugins/md-plugin-title';
32
+ import type { MarkdownItEnv } from '@md-plugins/shared';
33
+
34
+ const md = new MarkdownIt();
35
+ md.use(titlePlugin);
36
+
37
+ const markdownContent = `
38
+ # This is the Page Title
39
+
40
+ Some content here.
41
+ `;
42
+
43
+ const env: MarkdownItEnv = {};
44
+ const renderedOutput = md.render(markdownContent, env);
45
+
46
+ console.log('Rendered Output:', renderedOutput);
47
+ console.log('Extracted Title:', env.title);
48
+ ```
49
+
50
+ ### Example Output
51
+
52
+ For the example above, the `env` will contain the following:
53
+
54
+ ```json
55
+ {
56
+ "title": "This is the Page Title"
57
+ }
58
+ ```
59
+
60
+ And the rendered Markdown output will appear as usual:
61
+
62
+ ```html
63
+ <h1>This is the Page Title</h1>
64
+ <p>Some content here.</p>
65
+ ```
66
+
67
+ ## Options
68
+
69
+ The `md-plugin-title` plugin does not currently accept configuration options. It automatically extracts the first `<h1>` element.
70
+
71
+ ## Note
72
+
73
+ This plugin is not needed is using the `@md-plugins/md-plugin-frontmatter` plugin.
74
+
75
+ ## Testing
76
+
77
+ Run the tests to ensure the plugin behaves as expected:
78
+
79
+ ```bash
80
+ pnpm test
81
+ ```
82
+
83
+ ## License
84
+
85
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
@@ -0,0 +1,20 @@
1
+ import { PluginSimple } from 'markdown-it';
2
+
3
+ /**
4
+ * Get markdown page title info
5
+ *
6
+ * Extract it into env
7
+ */
8
+ declare const titlePlugin: PluginSimple;
9
+
10
+ declare module '@md-plugins/shared' {
11
+ interface MarkdownItEnv {
12
+ /**
13
+ * The title that extracted by `md-plugin-title`
14
+ */
15
+ title?: string;
16
+ heading?: boolean;
17
+ }
18
+ }
19
+
20
+ export { titlePlugin };
@@ -0,0 +1,20 @@
1
+ import { PluginSimple } from 'markdown-it';
2
+
3
+ /**
4
+ * Get markdown page title info
5
+ *
6
+ * Extract it into env
7
+ */
8
+ declare const titlePlugin: PluginSimple;
9
+
10
+ declare module '@md-plugins/shared' {
11
+ interface MarkdownItEnv {
12
+ /**
13
+ * The title that extracted by `md-plugin-title`
14
+ */
15
+ title?: string;
16
+ heading?: boolean;
17
+ }
18
+ }
19
+
20
+ export { titlePlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,16 @@
1
+ import { resolveTitleFromToken } from '@md-plugins/shared';
2
+
3
+ const titlePlugin = (md) => {
4
+ const render = md.renderer.render.bind(md.renderer);
5
+ md.renderer.render = (tokens, options, env) => {
6
+ const tokenIdx = tokens.findIndex((token) => token.tag === "h1");
7
+ env.title = tokenIdx > -1 ? resolveTitleFromToken(tokens[tokenIdx + 1], {
8
+ shouldAllowHtml: false,
9
+ shouldEscapeText: false
10
+ }) : "";
11
+ env.heading = !!env.title;
12
+ return render(tokens, options, env);
13
+ };
14
+ };
15
+
16
+ export { titlePlugin };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@md-plugins/md-plugin-title",
3
+ "version": "0.1.0-alpha.1",
4
+ "description": "A markdown-it plugin for getting Title information.",
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
+ }