@type32/codemirror-rich-obsidian-editor 0.0.20 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TocEntry } from '../editor/types/editor-types.js';
|
|
1
2
|
export declare function useDocumentUtils(): {
|
|
2
3
|
getWordCount: (text: string) => number;
|
|
3
4
|
getLineCount: (text: string) => number;
|
|
@@ -6,4 +7,5 @@ export declare function useDocumentUtils(): {
|
|
|
6
7
|
getParagraphs: (text: string) => number;
|
|
7
8
|
getAvgWordLength: (text: string) => number;
|
|
8
9
|
isEmpty: (text: string) => boolean;
|
|
10
|
+
getTableOfContents: (text: string) => TocEntry[];
|
|
9
11
|
};
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { useAlfaaz } from "./useAlfaaz.js";
|
|
2
|
+
import { markdown } from "@codemirror/lang-markdown";
|
|
3
|
+
import { GFM } from "@lezer/markdown";
|
|
4
|
+
import { CustomOFM } from "../editor/lezer-parsers/customOFMParsers.js";
|
|
2
5
|
export function useDocumentUtils() {
|
|
3
6
|
const alfaaz = useAlfaaz();
|
|
4
7
|
function getWordCount(text) {
|
|
@@ -26,6 +29,34 @@ export function useDocumentUtils() {
|
|
|
26
29
|
function isEmpty(text) {
|
|
27
30
|
return text.trim().length === 0;
|
|
28
31
|
}
|
|
32
|
+
function getTableOfContents(text) {
|
|
33
|
+
const toc = [];
|
|
34
|
+
if (!text) return toc;
|
|
35
|
+
const tree = markdown({
|
|
36
|
+
extensions: [GFM, CustomOFM, { remove: ["SetextHeading"] }]
|
|
37
|
+
}).language.parser.parse(text);
|
|
38
|
+
tree.iterate({
|
|
39
|
+
enter: (node) => {
|
|
40
|
+
if (node.name.startsWith("ATXHeading")) {
|
|
41
|
+
const levelMatch = node.name.match(/ATXHeading(\d)/);
|
|
42
|
+
if (levelMatch && levelMatch[1]) {
|
|
43
|
+
const level = parseInt(levelMatch[1], 10);
|
|
44
|
+
const headerMark = node.node.getChild("HeaderMark");
|
|
45
|
+
if (headerMark) {
|
|
46
|
+
const from = headerMark.to + 1;
|
|
47
|
+
const to = node.to;
|
|
48
|
+
const textContent = text.slice(from, to).trim();
|
|
49
|
+
toc.push({
|
|
50
|
+
level,
|
|
51
|
+
text: textContent
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return toc;
|
|
59
|
+
}
|
|
29
60
|
return {
|
|
30
61
|
getWordCount,
|
|
31
62
|
getLineCount,
|
|
@@ -33,6 +64,7 @@ export function useDocumentUtils() {
|
|
|
33
64
|
getReadingTime,
|
|
34
65
|
getParagraphs,
|
|
35
66
|
getAvgWordLength,
|
|
36
|
-
isEmpty
|
|
67
|
+
isEmpty,
|
|
68
|
+
getTableOfContents
|
|
37
69
|
};
|
|
38
70
|
}
|