|
1 | 1 | import path from "path";
|
2 |
| - |
3 | 2 | import {remark} from "remark";
|
4 |
| - |
5 |
| -import remarkRehype from "remark-rehype"; |
6 | 3 | import remarkBootstrapIcon from "./remark/bootstrap-icon.js";
|
7 |
| - |
8 |
| -import {rehypeLinkActive} from "./rehype/link-active.js"; |
9 |
| -import rehypeStringify from "rehype-stringify"; |
10 |
| - |
| 4 | +import remarkRehype from "remark-rehype"; |
| 5 | +import {rehypeLinkActive, urlMatches} from "./rehype/link-active.js"; |
11 | 6 | import {visit} from "unist-util-visit";
|
| 7 | +import {visitParents} from "unist-util-visit-parents"; |
12 | 8 | import {toString} from "hast-util-to-string";
|
| 9 | +import rehypeStringify from "rehype-stringify"; |
13 | 10 |
|
14 | 11 | /**
|
15 | 12 | * @param relative {string}
|
@@ -45,25 +42,71 @@ function findSidebar(fs, dir, root) {
|
45 | 42 | }
|
46 | 43 |
|
47 | 44 | /**
|
48 |
| - * @param content {string} |
49 |
| - * @param meta {Object} |
50 |
| - * @param loaderContext {import('webpack').LoaderContext<Object> & { data: { [key: string]: any } | string }} |
| 45 | + * |
| 46 | + * @param activePath {string} |
| 47 | + * @return {import('unified').Processor} |
51 | 48 | */
|
52 |
| -export default function (content, meta, loaderContext) { |
53 |
| - const {resourcePath, data, context, rootContext, fs} = loaderContext; |
54 |
| - const sidebarPath = findSidebar(fs, path.dirname(resourcePath), rootContext); |
55 |
| - const sidebarContent = sidebarPath && fs.readFileSync(sidebarPath, 'utf-8').trim(); |
56 |
| - const processor = remark() |
| 49 | +export function sidebarProcessor(activePath) { |
| 50 | + return remark() |
57 | 51 | .use(remarkBootstrapIcon)
|
58 | 52 | .use(remarkRehype, {allowDangerousHtml: true})
|
59 |
| - .use(rehypeLinkActive, {active: getPagePath(path.relative(rootContext, resourcePath))}) |
| 53 | + .use(rehypeLinkActive, {active: activePath}) |
| 54 | + .use(() => { |
| 55 | + /** |
| 56 | + * |
| 57 | + * @param li {import('hast').Element} |
| 58 | + */ |
| 59 | + function removeSubList(li) { |
| 60 | + const ulIndex = li.children.findIndex(({tagName}) => tagName === 'ul'); |
| 61 | + if (ulIndex < 0) { |
| 62 | + return; |
| 63 | + } |
| 64 | + li.children.splice(ulIndex, 1); |
| 65 | + } |
| 66 | + |
| 67 | + return (tree, vfile) => { |
| 68 | + |
| 69 | + visitParents(tree, node => node.tagName === 'li', (li, ancestors) => { |
| 70 | + const depth = ancestors.filter(node => node.tagName === 'ul').length; |
| 71 | + if (depth !== 2) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + function linkMatchesActive(a) { |
| 76 | + const href = a.properties?.href; |
| 77 | + if (!href) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + return urlMatches(href, activePath); |
| 81 | + } |
| 82 | + |
| 83 | + let active = false; |
| 84 | + visit(li, node => node.tagName === 'a' && linkMatchesActive(node), () => active = true); |
| 85 | + if (!active) { |
| 86 | + removeSubList(li); |
| 87 | + } |
| 88 | + }); |
| 89 | + }; |
| 90 | + }) |
60 | 91 | .use(() => (tree, vfile) => {
|
61 |
| - visit(tree, e => e.tagName === 'h1', (h1, index, parent) => { |
| 92 | + visit(tree, node => node.tagName === 'h1', (h1, index, parent) => { |
62 | 93 | vfile.data.title = toString(h1);
|
63 | 94 | parent.children[index] = {type: 'comment', value: vfile.data.title};
|
64 | 95 | });
|
65 | 96 | })
|
66 |
| - .use(rehypeStringify, {allowDangerousCharacters: true, allowDangerousHtml: true}); |
| 97 | + .use(rehypeStringify, {allowDangerousCharacters: true, allowDangerousHtml: true}) |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @param content {string} |
| 102 | + * @param meta {Object} |
| 103 | + * @param loaderContext {import('webpack').LoaderContext<Object> & { data: { [key: string]: any } | string }} |
| 104 | + */ |
| 105 | +export default function (content, meta, loaderContext) { |
| 106 | + const {resourcePath, data, context, rootContext, fs} = loaderContext; |
| 107 | + const sidebarPath = findSidebar(fs, path.dirname(resourcePath), rootContext); |
| 108 | + const sidebarContent = sidebarPath && fs.readFileSync(sidebarPath, 'utf-8').trim(); |
| 109 | + const processor = sidebarProcessor(getPagePath(path.relative(rootContext, resourcePath))); |
67 | 110 | /** @type {VFile|undefined} */
|
68 | 111 | const sidebar = sidebarContent && processor.processSync(sidebarContent);
|
69 | 112 | Object.assign(data, meta, sidebar && {sidebar: sidebar.data.title || 'Pages'});
|
|
0 commit comments