@md-plugins/md-plugin-headers 0.1.0-alpha.28 → 0.1.0-alpha.29

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.
Files changed (2) hide show
  1. package/dist/index.mjs +51 -30
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -3,74 +3,95 @@ const rCombining = /[\u0300-\u036F]/g;
3
3
  const rControl = /[\u0000-\u001f]/g;
4
4
  const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’<>,.?/]+/g;
5
5
  const slugify = (str) => str.trim().replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase().normalize("NFKD").replace(rCombining, "").replace(andRE, "-and-").replace(rControl, "-").replace(rSpecial, "-").replace(/[^a-z0-9-]+/g, "").replace(/([a-z])(\d)/g, "$1-$2").replace(/(\d)([a-z])/g, "$1-$2").replace(/-{2,}/g, "-").replace(/(^-|-$)/g, "").replace(/^(\d)/, "_$1");
6
+ function resolvePluginOptions(options, key, defaults) {
7
+ if (options && typeof options === "object" && key in options) {
8
+ return { ...defaults, ...options[key] };
9
+ }
10
+ return { ...defaults, ...options };
11
+ }
6
12
 
7
- const titleRE = /<\/?[^>]+(>|$)/g;
8
- const apiRE = /^<MarkdownApi /;
9
- const apiNameRE = /(?:file|name)="([^"]+)"/;
10
- const exampleRE = /^<MarkdownExample(?:\s+title="([^"]*)")?\s*/;
11
- function parseContent(str, slugify$1 = slugify, format = (_str) => _str) {
12
- const title = String(str).replace(titleRE, "").trim();
13
+ const DEFAULT_HEADERS_PLUGIN_OPTIONS = {
14
+ level: [2, 3],
15
+ slugify: slugify,
16
+ // Default to an identity function if no formatter is provided.
17
+ format: (str) => str,
18
+ shouldAllowNested: false,
19
+ shouldAllowApi: true,
20
+ shouldAllowExample: true
21
+ };
22
+ function parseContent(str, slugify, format) {
23
+ const title = String(str).replace(/<\/?[^>]+(>|$)/g, "").trim();
13
24
  return {
14
- id: slugify$1(title),
15
- title: format(title) ?? title
25
+ id: slugify(title),
26
+ title: format(title)
16
27
  };
17
28
  }
18
- const headersPlugin = (md, {
19
- level = [2, 3],
20
- slugify: slugify$1 = slugify,
21
- format,
22
- shouldAllowApi = true,
23
- shouldAllowExample = true
24
- } = {}) => {
29
+ const headersPlugin = (md, options) => {
30
+ const {
31
+ level = DEFAULT_HEADERS_PLUGIN_OPTIONS.level,
32
+ slugify = DEFAULT_HEADERS_PLUGIN_OPTIONS.slugify,
33
+ format = DEFAULT_HEADERS_PLUGIN_OPTIONS.format,
34
+ shouldAllowApi = DEFAULT_HEADERS_PLUGIN_OPTIONS.shouldAllowApi,
35
+ shouldAllowExample = DEFAULT_HEADERS_PLUGIN_OPTIONS.shouldAllowExample,
36
+ shouldAllowNested = DEFAULT_HEADERS_PLUGIN_OPTIONS.shouldAllowNested
37
+ } = resolvePluginOptions(
38
+ options,
39
+ "headersPlugin",
40
+ DEFAULT_HEADERS_PLUGIN_OPTIONS
41
+ );
25
42
  const originalHeadingOpen = md.renderer.rules.heading_open;
26
43
  const originalHtmlBlock = md.renderer.rules.html_block;
27
- md.renderer.rules.heading_open = (tokens, idx, options, env, self) => {
44
+ md.renderer.rules.heading_open = (tokens, idx, options2, env, self) => {
28
45
  const token = tokens[idx];
29
46
  if (!token) {
30
- return self.renderToken(tokens, idx, options);
47
+ return self.renderToken(tokens, idx, options2);
31
48
  }
32
49
  const headerLevel = Number.parseInt(token.tag.slice(1), 10);
33
50
  const contentToken = tokens[idx + 1];
34
51
  const content = contentToken && contentToken.children ? contentToken.children.reduce((acc, t) => acc + t.content, "") : "";
35
- const { id, title } = parseContent(content, slugify$1, format);
52
+ const { id, title } = parseContent(content, slugify, format);
36
53
  token.attrSet("id", id);
37
54
  token.attrSet("class", `markdown-heading markdown-${token.tag}`);
38
55
  token.attrSet("@click", `copyHeading(\`${id}\`)`);
39
56
  env.toc = env.toc || [];
40
- if (level.includes(headerLevel)) {
41
- if (headerLevel === level[0]) {
57
+ if ((level ?? []).includes(headerLevel)) {
58
+ if (headerLevel === (level ?? [])[0]) {
42
59
  env.toc.push({ id, title });
43
60
  } else {
44
61
  env.toc.push({ id, title, sub: true });
45
62
  }
46
63
  }
47
64
  if (typeof originalHeadingOpen === "function") {
48
- return originalHeadingOpen(tokens, idx, options, env, self);
65
+ return originalHeadingOpen(tokens, idx, options2, env, self);
49
66
  }
50
- return self.renderToken(tokens, idx, options);
67
+ return self.renderToken(tokens, idx, options2);
51
68
  };
52
- md.renderer.rules.html_block = (tokens, idx, options, env, self) => {
69
+ md.renderer.rules.html_block = (tokens, idx, options2, env, self) => {
53
70
  const token = tokens[idx];
54
71
  if (!token) {
55
72
  return "";
56
73
  }
57
74
  env.toc = env.toc || [];
58
- if (shouldAllowApi && apiRE.test(token.content)) {
59
- const match = apiNameRE.exec(token.content);
75
+ if (shouldAllowApi && /^<MarkdownApi\s/.test(token.content)) {
76
+ const match = /(?:file|name)="([^"]+)"/.exec(token.content);
60
77
  if (match !== null) {
61
78
  const title = `${match[1]} API`;
62
- env.toc.push({ id: slugify$1(title), title, deep: true });
79
+ if (slugify) {
80
+ env.toc.push({ id: slugify(title), title, deep: true });
81
+ }
63
82
  }
64
83
  }
65
- if (shouldAllowExample && exampleRE.test(token.content)) {
66
- const match = token.content.match(exampleRE);
84
+ if (shouldAllowExample && /^<MarkdownExample(?:\s+title="([^"]*)")?\s*/.test(token.content)) {
85
+ const match = token.content.match(/^<MarkdownExample(?:\s+title="([^"]*)")?\s*/);
67
86
  if (match !== null) {
68
87
  const title = match[1] ?? "Example";
69
- env.toc.push({ id: slugify$1("example-" + title), title, deep: true });
88
+ if (slugify) {
89
+ env.toc.push({ id: slugify("example-" + title), title, deep: true });
90
+ }
70
91
  }
71
92
  }
72
93
  if (typeof originalHtmlBlock === "function") {
73
- return originalHtmlBlock(tokens, idx, options, env, self);
94
+ return originalHtmlBlock(tokens, idx, options2, env, self);
74
95
  }
75
96
  return token.content;
76
97
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@md-plugins/md-plugin-headers",
3
- "version": "0.1.0-alpha.28",
3
+ "version": "0.1.0-alpha.29",
4
4
  "description": "A markdown-it plugin for handling headers (H1-H6).",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -35,7 +35,7 @@
35
35
  "devDependencies": {
36
36
  "markdown-it": "^14.1.0",
37
37
  "@types/markdown-it": "^14.1.2",
38
- "@md-plugins/shared": "0.1.0-alpha.28"
38
+ "@md-plugins/shared": "0.1.0-alpha.29"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "markdown-it": "^14.1.0"