|
| 1 | +// @ts-check |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const logger = require('loglevel'); |
| 5 | +const visit = require('unist-util-visit'); |
| 6 | +const setup = require('./setup'); |
| 7 | +const getPossibleThemes = require('./getPossibleThemes'); |
| 8 | +const createCodeNodeRegistry = require('./createCodeNodeRegistry'); |
| 9 | +const parseCodeFenceInfo = require('./parseCodeFenceInfo'); |
| 10 | +const parseCodeSpanInfo = require('./parseCodeSpanInfo'); |
| 11 | +const getCodeBlockGraphQLDataFromRegistry = require('./graphql/getCodeBlockDataFromRegistry'); |
| 12 | +const getCodeSpanGraphQLDataFromRegistry = require('./graphql/getCodeSpanDataFromRegistry'); |
| 13 | +const { registerCodeBlock, registerCodeSpan } = require('./registerCodeNode'); |
| 14 | +const { getScope } = require('./storeUtils'); |
| 15 | +const { createStyleElement } = require('./factory/html'); |
| 16 | +const { renderHTML } = require('./renderers/html'); |
| 17 | +const { createOnce } = require('./utils'); |
| 18 | +const createGetRegistry = require('./createGetRegistry'); |
| 19 | +const styles = fs.readFileSync(path.resolve(__dirname, '../styles.css'), 'utf8'); |
| 20 | + |
| 21 | +class Cache { |
| 22 | + constructor() { |
| 23 | + this.cache = new Map(); |
| 24 | + } |
| 25 | + async set(key, value) { |
| 26 | + this.cache.set(key, value); |
| 27 | + } |
| 28 | + async get(key) { |
| 29 | + return this.cache.get(key); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const once = createOnce(); |
| 34 | +const cache = new Cache(); |
| 35 | +const getRegistry = createGetRegistry(); |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {PluginOptions=} options |
| 39 | + */ |
| 40 | +function remarkPlugin(options = {}) { |
| 41 | + return async function(tree) { |
| 42 | + const { |
| 43 | + theme, |
| 44 | + wrapperClassName, |
| 45 | + languageAliases, |
| 46 | + extensions, |
| 47 | + getLineClassName, |
| 48 | + injectStyles, |
| 49 | + replaceColor, |
| 50 | + logLevel, |
| 51 | + getLineTransformers, |
| 52 | + inlineCode, |
| 53 | + ...rest |
| 54 | + } = await setup(options, undefined, cache, once); |
| 55 | + |
| 56 | + const lineTransformers = getLineTransformers( |
| 57 | + { |
| 58 | + theme, |
| 59 | + wrapperClassName, |
| 60 | + languageAliases, |
| 61 | + extensions, |
| 62 | + getLineClassName, |
| 63 | + injectStyles, |
| 64 | + replaceColor, |
| 65 | + logLevel, |
| 66 | + inlineCode, |
| 67 | + ...rest |
| 68 | + }, |
| 69 | + cache |
| 70 | + ); |
| 71 | + |
| 72 | + // 1. Gather all code fence nodes from Markdown AST. |
| 73 | + |
| 74 | + /** @type {(MDASTNode<'code'> | MDASTNode<'inlineCode'>)[]} */ |
| 75 | + const nodes = []; |
| 76 | + visit( |
| 77 | + tree, |
| 78 | + ({ type }) => type === 'code' || (inlineCode && type === 'inlineCode'), |
| 79 | + node => { |
| 80 | + nodes.push(node); |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + // 2. For each code fence found, parse its header, determine what themes it will use, |
| 85 | + // and register its contents with a central code block registry, performing tokenization |
| 86 | + // along the way. |
| 87 | + |
| 88 | + /** @type {CodeNodeRegistry<MDASTNode<'code' | 'inlineCode'>>} */ |
| 89 | + const codeNodeRegistry = createCodeNodeRegistry(); |
| 90 | + for (const node of nodes) { |
| 91 | + /** @type {string} */ |
| 92 | + const text = node.value || (node.children && node.children[0] && node.children[0].value); |
| 93 | + if (!text) continue; |
| 94 | + const { languageName, meta, text: parsedText = text } = |
| 95 | + node.type === 'code' |
| 96 | + ? parseCodeFenceInfo(node.lang ? node.lang.toLowerCase() : '', node.meta) |
| 97 | + : parseCodeSpanInfo(text, inlineCode.marker); |
| 98 | + |
| 99 | + if (node.type === 'inlineCode' && !languageName) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + const grammarCache = await cache.get('grammars'); |
| 104 | + const scope = getScope(languageName, grammarCache, languageAliases); |
| 105 | + if (!scope && languageName) { |
| 106 | + logger.warn( |
| 107 | + `Encountered unknown language '${languageName}'. ` + |
| 108 | + `If '${languageName}' is an alias for a supported language, ` + |
| 109 | + `use the 'languageAliases' plugin option to map it to the canonical language name.` |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + const nodeData = /** @type {CodeBlockData | CodeSpanData} */ ({ |
| 114 | + node, |
| 115 | + language: languageName, |
| 116 | + parsedOptions: meta |
| 117 | + }); |
| 118 | + |
| 119 | + const possibleThemes = await getPossibleThemes( |
| 120 | + node.type === 'inlineCode' ? inlineCode.theme || theme : theme, |
| 121 | + await cache.get('themes'), |
| 122 | + undefined, |
| 123 | + nodeData |
| 124 | + ); |
| 125 | + |
| 126 | + if (node.type === 'inlineCode') { |
| 127 | + await registerCodeSpan( |
| 128 | + codeNodeRegistry, |
| 129 | + node, |
| 130 | + possibleThemes, |
| 131 | + () => getRegistry(cache, scope), |
| 132 | + scope, |
| 133 | + parsedText, |
| 134 | + languageName, |
| 135 | + cache |
| 136 | + ); |
| 137 | + } else { |
| 138 | + await registerCodeBlock( |
| 139 | + codeNodeRegistry, |
| 140 | + node, |
| 141 | + possibleThemes, |
| 142 | + () => getRegistry(cache, scope), |
| 143 | + lineTransformers, |
| 144 | + scope, |
| 145 | + parsedText, |
| 146 | + languageName, |
| 147 | + meta, |
| 148 | + cache |
| 149 | + ); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // 3. For each code block/span registered, convert its tokenization and theme data |
| 154 | + // to a GraphQL-compatible representation, including HTML renderings. At the same |
| 155 | + // time, change the original code fence Markdown node to an HTML node and set |
| 156 | + // its value to the HTML rendering contained in the GraphQL node. |
| 157 | + |
| 158 | + codeNodeRegistry.forEachCodeBlock((codeBlock, node) => { |
| 159 | + const graphQLNode = getCodeBlockGraphQLDataFromRegistry( |
| 160 | + codeNodeRegistry, |
| 161 | + node, |
| 162 | + codeBlock, |
| 163 | + getWrapperClassName, |
| 164 | + getLineClassName |
| 165 | + ); |
| 166 | + |
| 167 | + // Update Markdown node |
| 168 | + /** @type {MDASTNode} */ |
| 169 | + (node).type = 'html'; |
| 170 | + node.value = graphQLNode.html; |
| 171 | + |
| 172 | + function getWrapperClassName() { |
| 173 | + return typeof wrapperClassName === 'function' |
| 174 | + ? wrapperClassName({ |
| 175 | + language: codeBlock.languageName, |
| 176 | + node, |
| 177 | + codeFenceNode: node, |
| 178 | + parsedOptions: codeBlock.meta |
| 179 | + }) |
| 180 | + : wrapperClassName; |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + codeNodeRegistry.forEachCodeSpan((codeSpan, node) => { |
| 185 | + const graphQLNode = getCodeSpanGraphQLDataFromRegistry(codeNodeRegistry, node, codeSpan, getClassName); |
| 186 | + |
| 187 | + // Update Markdown node |
| 188 | + /** @type {MDASTNode} */ |
| 189 | + (node).type = 'html'; |
| 190 | + node.value = graphQLNode.html; |
| 191 | + |
| 192 | + function getClassName() { |
| 193 | + return typeof inlineCode.className === 'function' |
| 194 | + ? inlineCode.className({ |
| 195 | + language: codeSpan.languageName, |
| 196 | + node |
| 197 | + }) |
| 198 | + : inlineCode.className; |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + // 4. Generate CSS rules for each theme used by one or more code blocks in the registry, |
| 203 | + // then append that CSS to the Markdown AST in an HTML node. |
| 204 | + |
| 205 | + const styleElement = createStyleElement( |
| 206 | + codeNodeRegistry.getAllPossibleThemes(), |
| 207 | + codeNodeRegistry.getTokenStylesForTheme, |
| 208 | + replaceColor, |
| 209 | + injectStyles ? styles : undefined |
| 210 | + ); |
| 211 | + |
| 212 | + if (styleElement) { |
| 213 | + tree.children.unshift({ type: 'html', value: renderHTML(styleElement) }); |
| 214 | + } |
| 215 | + }; |
| 216 | +} |
| 217 | + |
| 218 | +module.exports = remarkPlugin; |
0 commit comments