({
children: {
props: { className = 'language-js', children: code = '', meta },
},
noMargin,
}: {
children: React.ReactNode & {
props: {
className: string
children?: string
meta?: string
}
}
className?: string
noMargin?: boolean
})
| 19 | const htmlLang = html() |
| 20 | |
| 21 | const CodeBlock = function CodeBlock({ |
| 22 | children: { |
| 23 | props: { className = 'language-js', children: code = '', meta }, |
| 24 | }, |
| 25 | noMargin, |
| 26 | }: { |
| 27 | children: React.ReactNode & { |
| 28 | props: { |
| 29 | className: string |
| 30 | children?: string |
| 31 | meta?: string |
| 32 | } |
| 33 | } |
| 34 | className?: string |
| 35 | noMargin?: boolean |
| 36 | }) { |
| 37 | code = code.trimEnd() |
| 38 | let lang = jsxLang |
| 39 | if (className === 'language-css') { |
| 40 | lang = cssLang |
| 41 | } else if (className === 'language-html') { |
| 42 | lang = htmlLang |
| 43 | } |
| 44 | const tree = lang.language.parser.parse(code) |
| 45 | let tokenStarts = new Map() |
| 46 | let tokenEnds = new Map() |
| 47 | const highlightTheme = getSyntaxHighlight(CustomTheme) |
| 48 | highlightTree(tree as any, highlightTheme.match, (from, to, className) => { |
| 49 | tokenStarts.set(from, className) |
| 50 | tokenEnds.set(to, className) |
| 51 | }) |
| 52 | |
| 53 | const highlightedLines = new Map<number, { className: string; css: TwStyle[] }>() |
| 54 | const lines = code.split('\n') |
| 55 | const lineDecorators = getLineDecorators(code, meta) |
| 56 | for (let decorator of lineDecorators) { |
| 57 | highlightedLines.set(decorator.line - 1, { |
| 58 | className: decorator.className, |
| 59 | css: decorator.css, |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | const inlineDecorators = getInlineDecorators(code, meta) |
| 64 | const decoratorStarts = new Map<number, { className: string; css: TwStyle[] }>() |
| 65 | const decoratorEnds = new Map<number, { className: string; css: TwStyle[] }>() |
| 66 | for (let decorator of inlineDecorators) { |
| 67 | // Find where inline highlight starts and ends. |
| 68 | let decoratorStart = 0 |
| 69 | for (let i = 0; i < decorator.line - 1; i++) { |
| 70 | decoratorStart += lines[i].length + 1 |
| 71 | } |
| 72 | decoratorStart += decorator.startColumn |
| 73 | const decoratorEnd = decoratorStart + (decorator.endColumn - decorator.startColumn) |
| 74 | if (decoratorStarts.has(decoratorStart)) { |
| 75 | throw Error('Already opened decorator at ' + decoratorStart) |
| 76 | } |
| 77 | decoratorStarts.set(decoratorStart, { |
| 78 | className: decorator.className, |
nothing calls this directly
no test coverage detected
searching dependent graphs…