catmdx 0.1.1 → 0.2.0

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/index.d.ts CHANGED
@@ -162,32 +162,38 @@ declare namespace Tokens {
162
162
  }
163
163
  }
164
164
  //#endregion
165
- //#region src/walker.d.ts
166
- type WalkerToken = MarkdownMetadata | MarkdownSectionStart | MarkdownSectionEnd | MarkdownToken;
167
- interface MarkdownMetadata {
168
- type: 'metadata';
169
- metadata: string;
170
- }
171
- interface MarkdownSectionStart {
172
- type: 'section-start';
173
- path: Tokens.Heading[];
174
- levelParts: number[];
175
- startToken: Tokens.Heading;
165
+ //#region src/parser.d.ts
166
+ interface MarkdownInfo {
167
+ content: string;
168
+ metadata?: string;
169
+ root: MarkdownSection;
176
170
  }
177
- interface MarkdownSectionEnd {
178
- type: 'section-end';
179
- startToken: Tokens.Heading;
171
+ interface MarkdownSection {
172
+ headingToken?: Tokens.Heading;
173
+ numbers: number[];
174
+ tokens: Token[];
175
+ range: {
176
+ start: number;
177
+ end: number;
178
+ };
179
+ parent?: MarkdownSection;
180
+ children: MarkdownSection[];
180
181
  }
181
- interface MarkdownToken {
182
- type: 'token';
183
- token: Token;
184
- }
185
- declare function walkMarkdown(markdown: string): Generator<WalkerToken>;
182
+ //#endregion
183
+ //#region src/walker.d.ts
184
+ declare function walkMarkdown(sect: MarkdownSection): Generator<MarkdownSection, void, boolean | undefined>;
186
185
  //#endregion
187
186
  //#region src/toc.d.ts
188
- declare function getTOC(tokens: WalkerToken[]): string;
187
+ interface GetTOCOptions {
188
+ tocMetadata?: boolean;
189
+ }
190
+ declare function getTOC(md: MarkdownInfo, options?: GetTOCOptions): string;
189
191
  //#endregion
190
192
  //#region src/section.d.ts
191
- declare function getSection(tokens: WalkerToken[], titleOrNumber: string): string;
193
+ interface GetSectionOptions {
194
+ section: string[];
195
+ recursive?: boolean;
196
+ }
197
+ declare function getSections(md: MarkdownInfo, options: GetSectionOptions): string;
192
198
  //#endregion
193
- export { MarkdownMetadata, MarkdownSectionEnd, MarkdownSectionStart, MarkdownToken, WalkerToken, getSection, getTOC, walkMarkdown };
199
+ export { GetSectionOptions, GetTOCOptions, getSections, getTOC, walkMarkdown };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import { n as getTOC, r as walkMarkdown, t as getSection } from "./section-Cw5XNpYg.js";
1
+ import { n as getTOC, r as walkMarkdown, t as getSections } from "./section-BGi0lC4H.js";
2
2
 
3
- export { getSection, getTOC, walkMarkdown };
3
+ export { getSections, getTOC, walkMarkdown };
@@ -0,0 +1,58 @@
1
+ //#region src/walker.ts
2
+ function* walkMarkdown(sect) {
3
+ const queue = [sect];
4
+ for (let sect = queue.shift(); sect; sect = queue.shift()) {
5
+ if (yield sect) continue;
6
+ queue.unshift(...sect.children);
7
+ }
8
+ }
9
+
10
+ //#endregion
11
+ //#region src/content.ts
12
+ function getSectionContent(sect) {
13
+ return sect.tokens.map((token) => token.raw).join("");
14
+ }
15
+
16
+ //#endregion
17
+ //#region src/toc.ts
18
+ function getTOC(md, options) {
19
+ const toc = [];
20
+ for (const sect of walkMarkdown(md.root)) {
21
+ const content = getSectionContent(sect).trim();
22
+ if (!sect.parent && !content) continue;
23
+ let line = `${sect.numbers.length ? " ".repeat(sect.numbers.length - 1) : ""}* [${sect.headingToken?.text.replace(/[[\]]/g, "\\$&") || ""}](#${sect.numbers.join(".") || "ROOT"})`;
24
+ if (options?.tocMetadata) {
25
+ let metadata = `Line ${md.content.slice(0, sect.range.start).split("\n").length}`;
26
+ if (content.length) metadata += `, ${content.length} chs`;
27
+ else metadata += `, empty`;
28
+ line += ` - (${metadata})`;
29
+ }
30
+ toc.push(line);
31
+ }
32
+ return toc.join("\n");
33
+ }
34
+
35
+ //#endregion
36
+ //#region src/section.ts
37
+ function getSections(md, options) {
38
+ const iter = walkMarkdown(md.root);
39
+ let content = "";
40
+ for (let res = iter.next(); !res.done;) {
41
+ const sect = res.value;
42
+ const num = `#${sect.numbers.join(".") || "ROOT"}`;
43
+ const text = sect.headingToken?.text || "";
44
+ if (!options.section.some((target) => target === text || target === num)) {
45
+ res = iter.next();
46
+ continue;
47
+ }
48
+ for (const target of options.recursive ? walkMarkdown(sect) : [sect]) {
49
+ if (target.headingToken) content += target.headingToken.raw;
50
+ for (const token of target.tokens) content += token.raw;
51
+ }
52
+ res = iter.next(options.recursive);
53
+ }
54
+ return content;
55
+ }
56
+
57
+ //#endregion
58
+ export { getTOC as n, walkMarkdown as r, getSections as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catmdx",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/gzzhanghao/catmdx.git"