Skip to content

Commit

Permalink
try fix [#9] [#12]
Browse files Browse the repository at this point in the history
  • Loading branch information
Gk0Wk committed Oct 13, 2023
1 parent e0b7e0c commit 9b1be19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
61 changes: 33 additions & 28 deletions src/page-toc/PageTOCWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
IParseTreeNode,
IChangedTiddlers,
IWidgetInitialiseOptions,
IWikiASTNode,
} from 'tiddlywiki';
import { widget as Widget } from '$:/core/modules/widgets/widget.js';

Expand Down Expand Up @@ -120,7 +119,7 @@ class PageTOCWidget extends Widget {
'\\"',
)}"]`,
)
?.querySelectorAll?.(`.tc-tiddler-body > ${tag}`)?.[count];
?.querySelectorAll?.(`.tc-tiddler-body ${tag}`)?.[count];
if (!target) {
return;
}
Expand Down Expand Up @@ -187,7 +186,12 @@ class PageTOCWidget extends Widget {
return undefined;
}
const type = currentTiddler.fields.type || 'text/vnd.tiddlywiki';
if (type !== 'text/vnd.tiddlywiki' && type !== 'text/x-markdown') {
const deserializerType =
$tw.config.contentTypeInfo[type]?.deserializerType || type;
if (
deserializerType !== 'text/vnd.tiddlywiki' &&
deserializerType !== 'text/x-markdown'
) {
return undefined;
}
const headers: { tag: HeaderTag; count: number; text: string }[] = [];
Expand All @@ -199,33 +203,34 @@ class PageTOCWidget extends Widget {
h5: 0,
h6: 0,
};
const root = $tw.wiki.parseTiddler(this.tocTitle).tree;
if (root.length === 0) {
return undefined;
}
let contents = root;
// Parse params
while (['set', 'importvariables'].includes(contents[0]?.type)) {
contents = (contents[0] as IWikiASTNode).children ?? [];
}
$tw.utils.each([...contents], node => {
if (node.type !== 'element') {
return;

// parse AST tree
const ast = $tw.wiki.parseTiddler(this.tocTitle).tree;
// make widget tree
const rootWidget = $tw.wiki.makeWidget({ tree: ast });
// render and get dom tree
const container = $tw.fakeDocument.createElement('div');
rootWidget.render(container, null);

// dfs find all headers
const stack: Element[] = [container];
while (stack.length > 0) {
const node = stack.pop()!;
if (/^h[1-6]$/.test(node.tagName)) {
console.log(node);
headers.push({
tag: node.tagName as HeaderTag,
count: headersCount[node.tagName as HeaderTag]++,
text: node.textContent || '',
});
}
if (this.includeHeaderMap[(node as any).tag as HeaderTag] !== true) {
return;
if (Array.isArray(node.children)) {
for (let i = node.children.length - 1; i >= 0; i--) {
stack.push(node.children[i]);
}
}
// Render contents of header
contents.length = 1;
contents[0] = node;
const container = $tw.fakeDocument.createElement('div');
$tw.wiki.makeWidget({ tree: root }).render(container, null);
headers.push({
tag: (node as any).tag,
count: headersCount[(node as any).tag as HeaderTag]++,
text: container.textContent || '',
});
});
}

return {
title: this.tocTitle,
headers,
Expand Down
2 changes: 1 addition & 1 deletion src/page-toc/plugin.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.4",
"version": "0.0.5",
"type": "application/json",
"title": "$:/plugins/Gk0Wk/page-toc",
"plugin-type": "plugin",
Expand Down
3 changes: 0 additions & 3 deletions wiki/tiddlers/Draft of '111'.tid

This file was deleted.

5 changes: 4 additions & 1 deletion wiki/tiddlers/Page TOC.tid
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
created: 20211214153552380
creator: Sttot
icon: 🧾
modified: 20211215043051275
modified: 20231013185645188
modifier: Sttot
page-cover: https://unsplash.com/photos/xG8IQMqMITM/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MXx8dGFibGUlMjBvZiUyMGNvbnRlbnRzfHwwfHx8fDE2Mzk0OTcxNzk&force=true&w=1920
tags: MyPlugins ExcludeFromEmpty
Expand All @@ -27,6 +27,9 @@ type: text/vnd.tiddlywiki
:* 为控件添加`h1`,`h2`,`h3`,`h4`,`h5`和`h6`参数,可以自定义包含哪些标题;
:* 修复标题包括样式、控件和宏时显示不完全的问题;

; v0.0.5
:* 现在 TOC 终于可以支持 Markdown 格式,并且正确识别任意位置(包括 transclude)的标题,均可跳转

</$list>

<$list filter="[<lang>!search[zh]]">
Expand Down

0 comments on commit 9b1be19

Please sign in to comment.