Skip to content

Commit 4da48da

Browse files
committed
improved nav link
1 parent b507341 commit 4da48da

File tree

7 files changed

+93
-32
lines changed

7 files changed

+93
-32
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"ts-node": "^10.9.2",
7070
"typescript": "^5.5.2",
7171
"unist-util-visit": "^5.0.0",
72+
"unist-util-visit-parents": "^6.0.1",
7273
"vfile": "^6.0.1",
7374
"webpack": "^5.96.0",
7475
"webpack-cli": "^5.1.4",

src/views/guides/_sidebar.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# All Guides
22

33
* :bi-hash:[Rooting](/guides/rooting/)
4-
* [faultmanager](/guides/rooting/faultmanager/)
5-
* [dejavuln](/guides/rooting/dejavuln/)
6-
* [ASM](/guides/rooting/asm/)
7-
* [crashd](/guides/rooting/crashd/)
8-
* [WTA](/guides/rooting/wta/)
9-
* [RootMyTV](/guides/rooting/rootmytv/)
10-
* [GetMeNow](/guides/rooting/getmenow/)
11-
* [NVM](/guides/rooting/nvm/)
12-
4+
* Rooting Methods
5+
* [faultmanager](/guides/rooting/faultmanager/)
6+
* [dejavuln](/guides/rooting/dejavuln/)
7+
* [ASM](/guides/rooting/asm/)
8+
* [crashd](/guides/rooting/crashd/)
9+
* [WTA](/guides/rooting/wta/)
10+
* [RootMyTV](/guides/rooting/rootmytv/)
11+
* [GetMeNow](/guides/rooting/getmenow/)
12+
* [NVM](/guides/rooting/nvm/)
1313
* :bi-sliders:System Customization
14-
* [Multiview](/guides/customize/multiview/)
15-
* Fix Jailer Config
16-
* Move Apps to USB
17-
* Remove Store Demo
14+
* [Multiview](/guides/customize/multiview/)
15+
* [Fix Jailer Config](/guides/customize/jail-fix/)
16+
* Move Apps to USB
17+
* Remove Store Demo

src/views/guides/rooting/asm.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ASM
2+
3+
---
4+
5+
{{> stub }}

src/views/guides/rooting/dejavuln.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ by [throwaway96](https://github.com/throwaway96).
5454
### I'm seeing "This file cannot be played" error
5555

5656
This is not an error. The `.mp3` file is not a real audio file.
57+
1

webpack/markdown-to-page.js

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import path from "path";
2-
32
import {remark} from "remark";
4-
5-
import remarkRehype from "remark-rehype";
63
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";
116
import {visit} from "unist-util-visit";
7+
import {visitParents} from "unist-util-visit-parents";
128
import {toString} from "hast-util-to-string";
9+
import rehypeStringify from "rehype-stringify";
1310

1411
/**
1512
* @param relative {string}
@@ -45,25 +42,71 @@ function findSidebar(fs, dir, root) {
4542
}
4643

4744
/**
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}
5148
*/
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()
5751
.use(remarkBootstrapIcon)
5852
.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+
})
6091
.use(() => (tree, vfile) => {
61-
visit(tree, e => e.tagName === 'h1', (h1, index, parent) => {
92+
visit(tree, node => node.tagName === 'h1', (h1, index, parent) => {
6293
vfile.data.title = toString(h1);
6394
parent.children[index] = {type: 'comment', value: vfile.data.title};
6495
});
6596
})
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)));
67110
/** @type {VFile|undefined} */
68111
const sidebar = sidebarContent && processor.processSync(sidebarContent);
69112
Object.assign(data, meta, sidebar && {sidebar: sidebar.data.title || 'Pages'});

webpack/rehype/link-active.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import {visit} from "unist-util-visit";
22
import {trimEnd} from "lodash-es";
33

4+
/**
5+
*
6+
* @param a {string}
7+
* @param b {string}
8+
* @return {boolean}
9+
*/
10+
export function urlMatches(a, b) {
11+
return trimEnd(a, '/') === trimEnd(b, '/');
12+
}
13+
414
/**
515
* @param active{string}
616
* @return {Processor}
@@ -12,7 +22,7 @@ export function rehypeLinkActive({active}) {
1222
if (!href) {
1323
return false;
1424
}
15-
return trimEnd(href, '/') === active;
25+
return urlMatches(href, active);
1626
}
1727

1828
return (tree) => {

0 commit comments

Comments
 (0)