|
| 1 | +import type { Processor } from 'unified' |
| 2 | +import { unified } from 'unified' |
| 3 | +import remarkParse from 'remark-parse' |
| 4 | +import remark2rehype from 'remark-rehype' |
| 5 | +import remarkMDC from 'remark-mdc' |
| 6 | +import type { MarkdownOptions, MarkdownPlugin, MarkdownRoot } from '../types' |
| 7 | +import handlers from './handler' |
| 8 | +import compiler from './compiler' |
| 9 | +import { flattenNodeText } from './utils/ast' |
| 10 | +import { nodeTextContent } from './utils/node' |
| 11 | + |
| 12 | +const usePlugins = (plugins: Record<string, false | MarkdownPlugin>, stream: Processor) => { |
| 13 | + for (const plugin of Object.values(plugins)) { |
| 14 | + if (plugin) { |
| 15 | + const { instance, ...options } = plugin |
| 16 | + stream.use(instance, options) |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Generate text excerpt summary |
| 23 | + * @param {string} excerptContent - JSON AST generated from excerpt markdown. |
| 24 | + * @returns {string} concatinated excerpt |
| 25 | + */ |
| 26 | +export function generateDescription(excerptContent: MarkdownRoot) { |
| 27 | + return flattenNodeText(excerptContent) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Generate json body |
| 32 | + * @param {string} content - file content |
| 33 | + * @param {object} data - document data |
| 34 | + * @returns {object} JSON AST body |
| 35 | + */ |
| 36 | +export function generateBody(content: string, options: MarkdownOptions & { data: any }): Promise<MarkdownRoot> { |
| 37 | + const rehypeOptions: any = { |
| 38 | + handlers, |
| 39 | + allowDangerousHtml: true, |
| 40 | + } |
| 41 | + |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + const stream = unified().use(remarkParse) |
| 44 | + |
| 45 | + if (options.mdc) |
| 46 | + stream.use(remarkMDC) |
| 47 | + |
| 48 | + usePlugins(options.remarkPlugins, stream) |
| 49 | + stream.use(remark2rehype, rehypeOptions) |
| 50 | + usePlugins(options.rehypePlugins, stream) |
| 51 | + |
| 52 | + stream.use(compiler, options as any) |
| 53 | + stream.process( |
| 54 | + { |
| 55 | + value: content, |
| 56 | + data: options.data, |
| 57 | + }, |
| 58 | + (error, file) => { |
| 59 | + if (error) |
| 60 | + return reject(error) |
| 61 | + |
| 62 | + Object.assign(options.data, file?.data || {}) |
| 63 | + |
| 64 | + resolve(file?.result as MarkdownRoot) |
| 65 | + }, |
| 66 | + ) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +export function contentHeading(body: MarkdownRoot) { |
| 71 | + let title = '' |
| 72 | + let description = '' |
| 73 | + const children = body.children |
| 74 | + // top level `text` and `hr` can be ignored |
| 75 | + .filter(node => node.type !== 'text' && node.tag !== 'hr') |
| 76 | + |
| 77 | + if (children.length && children[0].tag === 'h1') { |
| 78 | + /** |
| 79 | + * Remove node |
| 80 | + */ |
| 81 | + const node = children.shift()! |
| 82 | + |
| 83 | + /** |
| 84 | + * Generate title |
| 85 | + */ |
| 86 | + title = nodeTextContent(node) |
| 87 | + } |
| 88 | + |
| 89 | + if (children.length && children[0].tag === 'p') { |
| 90 | + /** |
| 91 | + * Remove node |
| 92 | + */ |
| 93 | + const node = children.shift()! |
| 94 | + |
| 95 | + /** |
| 96 | + * Generate description |
| 97 | + */ |
| 98 | + description = nodeTextContent(node) |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + title, |
| 103 | + description, |
| 104 | + } |
| 105 | +} |
0 commit comments