| 33 | } |
| 34 | |
| 35 | export class Transformer implements ITransformer { |
| 36 | hooks: ITransformHooks; |
| 37 | |
| 38 | md: MarkdownIt; |
| 39 | |
| 40 | assetsMap: Record<string, IAssets> = {}; |
| 41 | |
| 42 | urlBuilder = new UrlBuilder(); |
| 43 | |
| 44 | plugins: ITransformPlugin[]; |
| 45 | |
| 46 | constructor( |
| 47 | plugins: Array< |
| 48 | ITransformPlugin | (() => ITransformPlugin) |
| 49 | > = builtInPlugins, |
| 50 | ) { |
| 51 | this.hooks = createTransformHooks(this); |
| 52 | this.plugins = plugins.map((plugin) => |
| 53 | typeof plugin === 'function' ? plugin() : plugin, |
| 54 | ); |
| 55 | |
| 56 | const assetsMap: typeof this.assetsMap = {}; |
| 57 | for (const { name, transform } of this.plugins) { |
| 58 | assetsMap[name] = transform(this.hooks); |
| 59 | } |
| 60 | this.assetsMap = assetsMap; |
| 61 | |
| 62 | const md = initializeMarkdownIt(); |
| 63 | this.md = md; |
| 64 | this.hooks.parser.call(md); |
| 65 | } |
| 66 | |
| 67 | transform( |
| 68 | content: string, |
| 69 | fallbackParserOptions?: Partial<IHtmlParserOptions>, |
| 70 | ): ITransformResult { |
| 71 | const context: ITransformContext = { |
| 72 | content, |
| 73 | features: {}, |
| 74 | parserOptions: fallbackParserOptions, |
| 75 | }; |
| 76 | this.hooks.beforeParse.call(this.md, context); |
| 77 | let { content: rawContent } = context; |
| 78 | if (context.frontmatterInfo) |
| 79 | rawContent = rawContent.slice(context.frontmatterInfo.offset); |
| 80 | const html = this.md.render(rawContent, {}); |
| 81 | this.hooks.afterParse.call(this.md, context); |
| 82 | const root = cleanNode(buildTree(html, context.parserOptions)); |
| 83 | root.content ||= `${context.frontmatter?.title || ''}`; |
| 84 | return { ...context, root }; |
| 85 | } |
| 86 | |
| 87 | resolveJS(item: JSItem) { |
| 88 | return patchJSItem(this.urlBuilder, item); |
| 89 | } |
| 90 | |
| 91 | resolveCSS(item: CSSItem) { |
| 92 | return patchCSSItem(this.urlBuilder, item); |
nothing calls this directly
no outgoing calls
no test coverage detected