Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
351 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from "react"; | ||
import { HeadingProps } from "react-markdown/lib/ast-to-react"; | ||
|
||
export const HeadingRender = (props: HeadingProps) => { | ||
const { node, children } = props; | ||
let text = ""; | ||
try { | ||
text = children[0] as string; | ||
} catch (err) { | ||
text = ""; | ||
} | ||
|
||
return React.createElement(node.tagName, { ["data-id"]: text }, text); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { useEffect, useState } from "react"; | ||
import throttle from "lodash/throttle"; | ||
import { NavItem } from "./tools"; | ||
|
||
import scroll from "react-scroll"; | ||
export default function (props: { items: NavItem[]; headingOffset: number }) { | ||
const { items } = props; | ||
const [currIndex, setCurrIndex] = useState(-1); | ||
const handleScroll = throttle((ev: Event) => { | ||
ev.stopPropagation(); | ||
ev.preventDefault(); | ||
|
||
let top: any = null; | ||
let topEl: any = null; | ||
let lastMin = 9999999999; | ||
for (const each of items) { | ||
const el: any = document.querySelector(`[data-id="${each.text}"]`); | ||
|
||
if (!topEl) { | ||
top = each; | ||
topEl = el; | ||
} | ||
if (el) { | ||
const scrollTop = | ||
window.pageYOffset || | ||
document.documentElement.scrollTop || | ||
document.body.scrollTop || | ||
0; | ||
const v = Math.abs(scrollTop + props.headingOffset - el.offsetTop); | ||
if (v <= lastMin) { | ||
lastMin = v; | ||
top = each; | ||
topEl = el; | ||
} | ||
} | ||
} | ||
setCurrIndex(top.index); | ||
|
||
// updateHash(top.text); | ||
}, 100); | ||
useEffect(() => { | ||
const el = document.querySelector(".markdown-navigation div.active"); | ||
if (el) { | ||
let to = (el as any)?.offsetTop; | ||
if (to <= props.headingOffset) { | ||
to = 0; | ||
} | ||
scroll.animateScroll.scrollTo(to, { | ||
containerId: "toc-container", | ||
smooth: true, | ||
delay: 0, | ||
spyThrottle: 0, | ||
}); | ||
} | ||
}, [currIndex, props.headingOffset]); | ||
//TODO 逻辑完善的 hash 更新 | ||
useEffect(() => { | ||
window.addEventListener("scroll", handleScroll); | ||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}); | ||
const res = []; | ||
for (const each of items) { | ||
const cls = `title-anchor title-level${each.level} ${ | ||
currIndex == each.index ? "active" : "" | ||
}`; | ||
res.push( | ||
<div | ||
key={each.index} | ||
className={cls} | ||
onClick={() => { | ||
const el: any = document.querySelector(`[data-id="${each.text}"]`); | ||
|
||
if (el) { | ||
let to = el.offsetTop - props.headingOffset; | ||
if (to <= 100) { | ||
to = 0; | ||
} | ||
scroll.animateScroll.scrollTo(to); | ||
} | ||
}} | ||
> | ||
{each.text} | ||
</div> | ||
); | ||
} | ||
return ( | ||
<> | ||
<div | ||
className="text-center text-lg font-medium mt-4 text-gray-700 dark:text-dark cursor-pointer" | ||
onClick={() => { | ||
scroll.animateScroll.scrollToTop(); | ||
}} | ||
> | ||
目录 | ||
</div> | ||
<div className="markdown-navigation">{res}</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useMemo } from "react"; | ||
import Core from "./core"; | ||
import { parseNavStructure } from "./tools"; | ||
|
||
export default function (props: { content: string; headingOffset?: number }) { | ||
const navData = useMemo(() => { | ||
return parseNavStructure(props.content.replace(/`#/g, "")); | ||
}, [props]); | ||
|
||
return <Core items={navData} headingOffset={props.headingOffset || 0} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
export interface NavItem { | ||
index: number; | ||
level: number; | ||
listNo: string; | ||
text: string; | ||
} | ||
export const parseNavStructure = (source: string): NavItem[] => { | ||
const contentWithoutCode = source | ||
.replace(/^[^#]+\n/g, "") | ||
.replace(/(?:[^\n#]+)#+\s([^#\n]+)\n*/g, "") // 匹配行内出现 # 号的情况 | ||
.replace(/^#\s[^#\n]*\n+/, "") | ||
.replace(/```[^`\n]*\n+[^```]+```\n+/g, "") | ||
.replace(/`([^`\n]+)`/g, "$1") | ||
.replace(/\*\*?([^*\n]+)\*\*?/g, "$1") | ||
.replace(/__?([^_\n]+)__?/g, "$1") | ||
.trim(); | ||
|
||
const pattOfTitle = /#+\s([^#\n]+)\n*/g; | ||
const matchResult = contentWithoutCode.match(pattOfTitle); | ||
|
||
if (!matchResult) { | ||
return []; | ||
} | ||
|
||
const navData = matchResult.map((r, i) => ({ | ||
index: i, | ||
//@ts-ignore | ||
level: r.match(/^#+/g)[0].length, | ||
text: r.replace(pattOfTitle, "$1"), | ||
})); | ||
|
||
let maxLevel = 0; | ||
navData.forEach((t) => { | ||
if (t.level > maxLevel) { | ||
maxLevel = t.level; | ||
} | ||
}); | ||
let matchStack = []; | ||
// 此部分重构,原有方法会出现次级标题后再次出现高级标题时,listNo重复的bug | ||
for (let i = 0; i < navData.length; i++) { | ||
const t: any = navData[i]; | ||
const { level } = t; | ||
while ( | ||
matchStack.length && | ||
matchStack[matchStack.length - 1].level > level | ||
) { | ||
matchStack.pop(); | ||
} | ||
if (matchStack.length === 0) { | ||
const arr = new Array(maxLevel).fill(0); | ||
arr[level - 1] += 1; | ||
matchStack.push({ | ||
level, | ||
arr, | ||
}); | ||
t.listNo = trimArrZero(arr).join("."); | ||
continue; | ||
} | ||
const { arr } = matchStack[matchStack.length - 1] as any; | ||
const newArr = arr.slice(); | ||
newArr[level - 1] += 1; | ||
matchStack.push({ | ||
level, | ||
arr: newArr, | ||
}); | ||
t.listNo = trimArrZero(newArr).join("."); | ||
} | ||
return navData as NavItem[]; | ||
}; | ||
|
||
const trimArrZero = (arr: any) => { | ||
let start, end; | ||
for (start = 0; start < arr.length; start++) { | ||
if (arr[start]) { | ||
break; | ||
} | ||
} | ||
for (end = arr.length - 1; end >= 0; end--) { | ||
if (arr[end]) { | ||
break; | ||
} | ||
} | ||
return arr.slice(start, end + 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.