@mdit-vue/plugin-title 0.4.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.4.0](https://github.com/mdit-vue/mdit-vue/compare/v0.3.1...v0.4.0) (2022-07-02)
7
+
8
+ ### Features
9
+
10
+ - **plugin-title:** add title plugin ([662e153](https://github.com/mdit-vue/mdit-vue/commit/662e153b74226df6db57531e71ab5f8d563da780))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present, mdit-vue Contributors
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,35 @@
1
+ # @mdit-vue/plugin-title
2
+
3
+ [![npm](https://badgen.net/npm/v/@mdit-vue/plugin-title)](https://www.npmjs.com/package/@mdit-vue/plugin-title)
4
+ [![license](https://badgen.net/github/license/mdit-vue/mdit-vue)](https://github.com/mdit-vue/mdit-vue/blob/main/LICENSE)
5
+
6
+ A [markdown-it](https://github.com/markdown-it/markdown-it) plugin to get page title.
7
+
8
+ - Extract title (the content of the first level-1 heading) into markdown-it `env.title`.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ npm i @mdit-vue/plugin-title
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import MarkdownIt from 'markdown-it';
20
+ import { titlePlugin } from '@mdit-vue/plugin-title';
21
+
22
+ const md = MarkdownIt({ html: true }).use(titlePlugin);
23
+ const env: MarkdownItEnv = {};
24
+
25
+ const rendered = md.render(
26
+ `\
27
+ # h1
28
+ ## h2
29
+ ### h3
30
+ `,
31
+ env,
32
+ );
33
+
34
+ console.log(env.title);
35
+ ```
@@ -0,0 +1,11 @@
1
+ import { defineBuildConfig } from 'unbuild';
2
+
3
+ export default defineBuildConfig({
4
+ clean: true,
5
+ declaration: true,
6
+ entries: ['src/index'],
7
+ rollup: {
8
+ emitCJS: true,
9
+ inlineDependencies: false,
10
+ },
11
+ });
package/dist/index.cjs ADDED
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const shared = require('@mdit-vue/shared');
6
+
7
+ const titlePlugin = (md) => {
8
+ let title;
9
+ md.core.ruler.push("resolveTitle", (state) => {
10
+ const tokenIdx = state.tokens.findIndex((token) => token.tag === "h1");
11
+ if (tokenIdx > -1) {
12
+ title = shared.resolveTitleFromToken(state.tokens[tokenIdx + 1], {
13
+ shouldAllowHtml: false,
14
+ shouldEscapeText: false
15
+ });
16
+ } else {
17
+ title = "";
18
+ }
19
+ return true;
20
+ });
21
+ const render = md.render.bind(md);
22
+ md.render = (src, env = {}) => {
23
+ const result = render(src, env);
24
+ env.title = title;
25
+ return result;
26
+ };
27
+ };
28
+
29
+ exports.titlePlugin = titlePlugin;
@@ -0,0 +1,16 @@
1
+ import { PluginSimple } from 'markdown-it';
2
+
3
+ declare module '@mdit-vue/shared' {
4
+ interface MarkdownItEnv {
5
+ title?: string;
6
+ }
7
+ }
8
+
9
+ /**
10
+ * Get markdown page title info
11
+ *
12
+ * Extract it to env
13
+ */
14
+ declare const titlePlugin: PluginSimple;
15
+
16
+ export { titlePlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,25 @@
1
+ import { resolveTitleFromToken } from '@mdit-vue/shared';
2
+
3
+ const titlePlugin = (md) => {
4
+ let title;
5
+ md.core.ruler.push("resolveTitle", (state) => {
6
+ const tokenIdx = state.tokens.findIndex((token) => token.tag === "h1");
7
+ if (tokenIdx > -1) {
8
+ title = resolveTitleFromToken(state.tokens[tokenIdx + 1], {
9
+ shouldAllowHtml: false,
10
+ shouldEscapeText: false
11
+ });
12
+ } else {
13
+ title = "";
14
+ }
15
+ return true;
16
+ });
17
+ const render = md.render.bind(md);
18
+ md.render = (src, env = {}) => {
19
+ const result = render(src, env);
20
+ env.title = title;
21
+ return result;
22
+ };
23
+ };
24
+
25
+ export { titlePlugin };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@mdit-vue/plugin-title",
3
+ "version": "0.4.0",
4
+ "description": "A markdown-it plugin to get markdown page title",
5
+ "keywords": [
6
+ "markdown-it",
7
+ "markdown-it-plugin",
8
+ "vue",
9
+ "title"
10
+ ],
11
+ "homepage": "https://github.com/mdit-vue",
12
+ "bugs": {
13
+ "url": "https://github.com/mdit-vue/mdit-vue/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/mdit-vue/mdit-vue.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "meteorlxy <meteor.lxy@foxmail.com>",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.mjs",
25
+ "require": "./dist/index.cjs"
26
+ }
27
+ },
28
+ "main": "./dist/index.cjs",
29
+ "module": "./dist/index.mjs",
30
+ "types": "./dist/index.d.ts",
31
+ "scripts": {
32
+ "build": "unbuild",
33
+ "test": "vitest"
34
+ },
35
+ "dependencies": {
36
+ "@mdit-vue/shared": "0.4.0",
37
+ "@types/markdown-it": "^12.2.3",
38
+ "markdown-it": "^13.0.1"
39
+ },
40
+ "devDependencies": {
41
+ "@types/markdown-it-emoji": "^2.0.2",
42
+ "c8": "^7.11.3",
43
+ "markdown-it-emoji": "^2.0.2",
44
+ "unbuild": "^0.7.4",
45
+ "vitest": "^0.16.0"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "gitHead": "744887e6f4733af8da2045625e72a476947cfc25"
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import './types';
2
+
3
+ export * from './title-plugin';
@@ -0,0 +1,35 @@
1
+ import { resolveTitleFromToken } from '@mdit-vue/shared';
2
+ import type { MarkdownItEnv } from '@mdit-vue/shared';
3
+ import type { PluginSimple } from 'markdown-it';
4
+
5
+ /**
6
+ * Get markdown page title info
7
+ *
8
+ * Extract it to env
9
+ */
10
+ export const titlePlugin: PluginSimple = (md): void => {
11
+ let title: string;
12
+
13
+ // push the rule to the end of the chain
14
+ // resolve title from the parsed tokens
15
+ md.core.ruler.push('resolveTitle', (state) => {
16
+ const tokenIdx = state.tokens.findIndex((token) => token.tag === 'h1');
17
+ if (tokenIdx > -1) {
18
+ title = resolveTitleFromToken(state.tokens[tokenIdx + 1], {
19
+ shouldAllowHtml: false,
20
+ shouldEscapeText: false,
21
+ });
22
+ } else {
23
+ title = '';
24
+ }
25
+ return true;
26
+ });
27
+
28
+ // extract title to env
29
+ const render = md.render.bind(md);
30
+ md.render = (src, env: MarkdownItEnv = {}) => {
31
+ const result = render(src, env);
32
+ env.title = title;
33
+ return result;
34
+ };
35
+ };
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export {};
2
+
3
+ declare module '@mdit-vue/shared' {
4
+ interface MarkdownItEnv {
5
+ title?: string;
6
+ }
7
+ }
@@ -0,0 +1,30 @@
1
+ import type { MarkdownItEnv } from '@mdit-vue/shared';
2
+ import MarkdownIt from 'markdown-it';
3
+ import emojiPlugin from 'markdown-it-emoji';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { titlePlugin } from '../src';
6
+
7
+ describe('@mdit-vue/plugin-title > title-plugin', () => {
8
+ const md = MarkdownIt().use(emojiPlugin).use(titlePlugin);
9
+
10
+ describe('should extract title from h1 heading', () => {
11
+ const testCases = [
12
+ ['# title from h1 :tada:', 'title from h1 🎉'],
13
+ ['# title from h1 `foobar`', 'title from h1 foobar'],
14
+ ];
15
+
16
+ testCases.forEach(([source, expected]) =>
17
+ it(source, () => {
18
+ const env: MarkdownItEnv = {};
19
+ md.render(source, env);
20
+ expect(env.title).toEqual(expected);
21
+ }),
22
+ );
23
+ });
24
+
25
+ it('should extract empty title', () => {
26
+ const env: MarkdownItEnv = {};
27
+ md.render('', env);
28
+ expect(env.title).toEqual('');
29
+ });
30
+ });