|
| 1 | +import { memo } from 'react'; |
| 2 | +import rehypeHighlight from 'rehype-highlight'; |
| 3 | +import { visit, SKIP } from 'unist-util-visit'; |
1 | 4 | import getProcessor from './getProcessor'; |
2 | 5 |
|
3 | | -import './style.css'; |
4 | 6 | import 'katex/dist/katex.css'; |
5 | 7 | import 'highlight.js/styles/base16/tomorrow.css'; |
6 | | -import { memo } from 'react'; |
| 8 | +import './style.css'; |
| 9 | +import './highlightFormatIssue.css'; |
| 10 | + |
| 11 | +function hastHeilightSpace() { |
| 12 | + const heilightSpaceElement: import('hast').Element = { |
| 13 | + type: 'element', |
| 14 | + tagName: 'span', |
| 15 | + properties: { className: ['articleViewer-heilightSpace'] }, |
| 16 | + children: [{ type: 'text', value: ' ' }] |
| 17 | + }; |
| 18 | + return (tree: import('hast').Root) => |
| 19 | + visit(tree, 'element', element => { |
| 20 | + if (element === heilightSpaceElement) return SKIP; |
| 21 | + if (element.tagName === 'code') return SKIP; |
| 22 | + let className = element.properties['className']; |
| 23 | + if (typeof className === 'string') className = [className]; |
| 24 | + if ( |
| 25 | + Array.isArray(className) && |
| 26 | + (className.includes('katex') || className.includes('katex-display')) |
| 27 | + ) |
| 28 | + return SKIP; |
| 29 | + const newChildren = new Array<import('hast').ElementContent>(); |
| 30 | + for (let i of element.children) { |
| 31 | + if (i.type !== 'text') { |
| 32 | + newChildren.push(i); |
| 33 | + continue; |
| 34 | + } |
| 35 | + i.value.split(' ').forEach((s, i) => { |
| 36 | + if (i) newChildren.push(heilightSpaceElement); |
| 37 | + newChildren.push({ type: 'text', value: s }); |
| 38 | + }); |
| 39 | + } |
| 40 | + element.children = newChildren; |
| 41 | + }); |
| 42 | +} |
7 | 43 |
|
8 | | -const processor = getProcessor(); |
| 44 | +const processor = getProcessor({ |
| 45 | + rehypePlugins: [hastHeilightSpace, rehypeHighlight] |
| 46 | +}); |
9 | 47 |
|
10 | 48 | const ArticleViewer = memo(function ({ |
11 | 49 | children: markdown |
|
0 commit comments