Skip to content

Commit

Permalink
feat: support heading links
Browse files Browse the repository at this point in the history
close #48
  • Loading branch information
gera2ld committed Dec 18, 2024
1 parent 62ca778 commit 4b91ae7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ document.addEventListener('click', (e) => {
const el = (e.target as HTMLElement)?.closest('a');
if (el) {
const href = el.getAttribute('href');
if (!href.includes('://')) {
if (href.startsWith('#')) {
const node = findHeading(href.slice(1));
if (node) highlightNode(node);
} else if (!href.includes('://')) {
vscode.postMessage({
type: 'openFile',
data: href,
Expand Down Expand Up @@ -98,6 +101,21 @@ function clickHandler(type: string) {
};
}

function findHeading(id: string) {
function dfs(node: INode) {
if (!/^h\d$/.test(node.payload.tag as string)) return false;
const normalizedId = node.content.trim().replace(/\W/g, '-').toLowerCase();
if (normalizedId === id) {
target = node;
return true;
}
return node.children?.some(dfs);
}
let target: INode | undefined;
dfs(root);
return target;
}

function findActiveNode(line: number) {
function dfs(node: INode) {
const [start, end] =
Expand Down

0 comments on commit 4b91ae7

Please sign in to comment.