@altano/remark-mdx-toc-with-slugs 0.0.1-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/README.md +69 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +56 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# remark-mdx-toc-with-slugs
|
|
2
|
+
|
|
3
|
+
This is a [remark](https://remark.js.org/) preset that performs the same transform as [remark-mdx-toc](https://github.com/DCsunset/remark-mdx-toc) plugin but exports the table of contents with an additional `slug` property.
|
|
4
|
+
|
|
5
|
+
So `remark-mdx-toc` exports this object:
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
[
|
|
9
|
+
{
|
|
10
|
+
depth: 2,
|
|
11
|
+
value: "Resource Acquisition Is Initialization (RAII)",
|
|
12
|
+
attributes: {},
|
|
13
|
+
children: [
|
|
14
|
+
{
|
|
15
|
+
depth: 3,
|
|
16
|
+
value: "Is There a Problem?",
|
|
17
|
+
attributes: {},
|
|
18
|
+
children: [],
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
depth: 3,
|
|
22
|
+
value: "C++ Classes",
|
|
23
|
+
attributes: {},
|
|
24
|
+
children: [],
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
And `remark-mdx-toc-with-slugs` modifies it to export this instead:
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
[
|
|
35
|
+
{
|
|
36
|
+
depth: 2,
|
|
37
|
+
value: "Resource Acquisition Is Initialization (RAII)",
|
|
38
|
+
attributes: {},
|
|
39
|
+
children: [
|
|
40
|
+
{
|
|
41
|
+
depth: 3,
|
|
42
|
+
value: "Is There a Problem?",
|
|
43
|
+
attributes: {},
|
|
44
|
+
children: [],
|
|
45
|
+
slug: "is-there-a-problem",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
depth: 3,
|
|
49
|
+
value: "C++ Classes",
|
|
50
|
+
attributes: {},
|
|
51
|
+
children: [],
|
|
52
|
+
slug: "c-classes",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
depth: 3,
|
|
56
|
+
value: "The Solution",
|
|
57
|
+
attributes: {},
|
|
58
|
+
children: [],
|
|
59
|
+
slug: "the-solution",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
slug: "resource-acquisition-is-initialization-raii",
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you don't need the slug, continue using [remark-mdx-toc](https://github.com/DCsunset/remark-mdx-toc).
|
|
68
|
+
|
|
69
|
+
Slugs are generated using [`github-slugger`](https://www.npmjs.com/package/github-slugger) and should therefore match the [`rehype-slug`](https://www.npmjs.com/package/rehype-slug) package
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as unist from 'unist';
|
|
2
|
+
import { RemarkMdxTocOptions } from 'remark-mdx-toc';
|
|
3
|
+
import { Plugin } from 'unified';
|
|
4
|
+
|
|
5
|
+
declare type RemarkMdxTocWithSlugsOptions = RemarkMdxTocOptions;
|
|
6
|
+
declare const preset: {
|
|
7
|
+
plugins: Plugin<[(RemarkMdxTocOptions | undefined)?], unist.Node<unist.Data>, unist.Node<unist.Data>>[];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { RemarkMdxTocWithSlugsOptions, preset as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { generate } from "astring";
|
|
3
|
+
import { valueToEstree } from "estree-util-value-to-estree";
|
|
4
|
+
import { EXIT, visit as estreeVisit } from "estree-util-visit";
|
|
5
|
+
import GithubSlugger from "github-slugger";
|
|
6
|
+
import { remarkMdxToc } from "remark-mdx-toc";
|
|
7
|
+
import { visit } from "unist-util-visit";
|
|
8
|
+
function sluggifyTocEntries(entries, headingSlugger = new GithubSlugger()) {
|
|
9
|
+
return entries.map((entry) => sluggifyTocEntry(entry, headingSlugger));
|
|
10
|
+
}
|
|
11
|
+
function sluggifyTocEntry(entry, headingSlugger) {
|
|
12
|
+
return {
|
|
13
|
+
...entry,
|
|
14
|
+
slug: headingSlugger.slug(entry.value),
|
|
15
|
+
children: sluggifyTocEntries(entry.children, headingSlugger)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function assertMdxjsEsmNode(node) {
|
|
19
|
+
if (node.type !== "mdxjsEsm") {
|
|
20
|
+
throw new Error(`Node was unexpected type: ${node.type}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
var remarkMdxTocWithSlugs = (options = {}) => {
|
|
24
|
+
const name = options.name ?? "toc";
|
|
25
|
+
return function mdxTocWithSlugsTransformer(ast) {
|
|
26
|
+
const mdast = ast;
|
|
27
|
+
visit(mdast, ["mdxjsEsm"], (mdxNode) => {
|
|
28
|
+
assertMdxjsEsmNode(mdxNode);
|
|
29
|
+
const estree = mdxNode.data?.estree;
|
|
30
|
+
if (estree) {
|
|
31
|
+
estreeVisit(estree, (esNode) => {
|
|
32
|
+
if (esNode.type === "VariableDeclarator" && esNode.id.type === "Identifier" && esNode.id.name === name) {
|
|
33
|
+
if (esNode.init) {
|
|
34
|
+
const tocExportString = generate(esNode.init);
|
|
35
|
+
const toc = JSON.parse(tocExportString);
|
|
36
|
+
const tocWithSlugs = sluggifyTocEntries(toc);
|
|
37
|
+
const newTree = valueToEstree(tocWithSlugs);
|
|
38
|
+
if (newTree.type === "ArrayExpression") {
|
|
39
|
+
esNode.init = newTree;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return EXIT;
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
var preset = {
|
|
51
|
+
plugins: [remarkMdxToc, remarkMdxTocWithSlugs]
|
|
52
|
+
};
|
|
53
|
+
var src_default = preset;
|
|
54
|
+
export {
|
|
55
|
+
src_default as default
|
|
56
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@altano/remark-mdx-toc-with-slugs",
|
|
3
|
+
"version": "0.0.1-alpha.1",
|
|
4
|
+
"description": "A remark plugin to generate a table of contents (including slugs) and convert it into MDX export",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
11
|
+
"dev": "tsup src/index.ts --watch --format esm --dts",
|
|
12
|
+
"lint": "TIMING=1 eslint src/**/*.ts* --fix",
|
|
13
|
+
"format": "prettier --write src",
|
|
14
|
+
"clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist"
|
|
15
|
+
},
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "Alan Norbauer",
|
|
18
|
+
"email": "altano@gmail.com",
|
|
19
|
+
"url": "https://alan.norbauer.com"
|
|
20
|
+
},
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/altano/npm-packages",
|
|
25
|
+
"directory": "packages/remark-mdx-toc-with-slugs"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/altano/npm-packages/tree/main/packages/remark-mdx-toc-with-slugs",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"keywords": [
|
|
32
|
+
"remark",
|
|
33
|
+
"mdx"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"astring": "^1.8.6",
|
|
37
|
+
"estree-util-value-to-estree": "^3.0.1",
|
|
38
|
+
"estree-util-visit": "^1.2.1",
|
|
39
|
+
"github-slugger": "^2.0.0",
|
|
40
|
+
"mdast-util-mdxjs-esm": "^1.3.1",
|
|
41
|
+
"remark-mdx-toc": "^0.3.1",
|
|
42
|
+
"unist-util-visit": "^4.1.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@altano/tsconfig": "*",
|
|
46
|
+
"@types/node": "^18.15.13"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|