forked from ksylvan/markdown-tree-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (67 loc) · 2.4 KB
/
Copy pathindex.js
File metadata and controls
77 lines (67 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* markdown-tree-parser
*
* A powerful JavaScript library for parsing and manipulating markdown files as tree structures.
* Built on top of the remark/unified ecosystem.
*
* @author Kayvan Sylvan <kayvan@sylvan.com>
* @version 1.0.0
* @license MIT
*/
import { MarkdownTreeParser } from './lib/markdown-parser.js';
// Export the main class
export { MarkdownTreeParser };
// Default export for convenience
export default MarkdownTreeParser;
// Export additional utilities that might be useful
export { unified } from 'unified';
export { visit } from 'unist-util-visit';
export { selectAll, select } from 'unist-util-select';
export { find } from 'unist-util-find';
/**
* Convenience function to create a new parser instance
* @param {Object} options - Configuration options for the parser
* @returns {MarkdownTreeParser} New parser instance
*/
export function createParser(options = {}) {
return new MarkdownTreeParser(options);
}
/**
* Quick utility to parse markdown and extract a section
* @param {string} markdown - Markdown content
* @param {string} sectionName - Section heading to extract
* @param {Object} options - Parser options
* @returns {Promise<string|null>} Extracted section as markdown or null
*/
export async function extractSection(markdown, sectionName, options = {}) {
const parser = new MarkdownTreeParser(options);
const tree = await parser.parse(markdown);
const section = parser.extractSection(tree, sectionName);
if (!section) {
return null;
}
return await parser.stringify(section);
}
/**
* Quick utility to get all headings from markdown
* @param {string} markdown - Markdown content
* @param {Object} options - Parser options
* @returns {Promise<Array>} Array of heading objects
*/
export async function getHeadings(markdown, options = {}) {
const parser = new MarkdownTreeParser(options);
const tree = await parser.parse(markdown);
return parser.getHeadingsList(tree);
}
/**
* Quick utility to generate table of contents
* @param {string} markdown - Markdown content
* @param {number} maxLevel - Maximum heading level (default: 3)
* @param {Object} options - Parser options
* @returns {Promise<string>} Table of contents as markdown
*/
export async function generateTOC(markdown, maxLevel = 3, options = {}) {
const parser = new MarkdownTreeParser(options);
const tree = await parser.parse(markdown);
return parser.generateTableOfContents(tree, maxLevel);
}