Skip to content

Commit 8fe26b3

Browse files
高亮文章空格
1 parent 1492a5f commit 8fe26b3

File tree

6 files changed

+61
-23
lines changed

6 files changed

+61
-23
lines changed

src/app/features/article/articleViewer/getProcessor.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import remarkGfm from 'remark-gfm';
66
import remarkParse from 'remark-parse';
77
import remarkMath from './remarkMath.js';
88
import remarkRehype from 'remark-rehype';
9-
import { unified } from 'unified';
9+
import { PluggableList, unified } from 'unified';
1010
import rehypeKatex from 'rehype-katex';
11-
import rehypeHighlight from 'rehype-highlight';
1211
import rehypeReact from 'rehype-react';
1312
import { visit } from 'unist-util-visit';
1413
import parsePath from 'parse-path';
@@ -22,15 +21,19 @@ const rehypeReactConfig: import('hast-util-to-jsx-runtime').Options = {
2221
jsxs: props.jsxs
2322
};
2423

25-
export default function getProcessor() {
24+
export default function getProcessor({
25+
remarkPlugins,
26+
rehypePlugins
27+
}: { remarkPlugins?: PluggableList; rehypePlugins?: PluggableList } = {}) {
2628
return unified()
2729
.use(remarkParse)
2830
.use(remarkGfm)
2931
.use(remarkMath)
32+
.use(remarkPlugins || [])
3033
.use(remarkRehype)
3134
.use(rehypeKatex)
3235
.use(hastBilibili)
33-
.use(rehypeHighlight)
36+
.use(rehypePlugins || [])
3437
.use(rehypeReact, rehypeReactConfig)
3538
.freeze();
3639
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.articleViewer-heilightSpace {
2+
background-color: #9f9;
3+
}

src/app/features/article/articleViewer/index.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1+
import { memo } from 'react';
2+
import rehypeHighlight from 'rehype-highlight';
3+
import { visit, SKIP } from 'unist-util-visit';
14
import getProcessor from './getProcessor';
25

3-
import './style.css';
46
import 'katex/dist/katex.css';
57
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+
}
743

8-
const processor = getProcessor();
44+
const processor = getProcessor({
45+
rehypePlugins: [hastHeilightSpace, rehypeHighlight]
46+
});
947

1048
const ArticleViewer = memo(function ({
1149
children: markdown

src/index.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import { createRoot } from 'react-dom/client';
33

44
import App from './app';
55

6-
declare const RUN: () => boolean;
7-
8-
if (RUN()) {
9-
document.body.innerHTML = '';
10-
const div = document.createElement('div');
11-
document.body.appendChild(div);
12-
div.id = 'app';
13-
createRoot(div).render(
14-
<React.StrictMode>
15-
<App />
16-
</React.StrictMode>
17-
);
18-
}
6+
document.body.innerHTML = '';
7+
const div = document.createElement('div');
8+
document.body.appendChild(div);
9+
div.id = 'app';
10+
createRoot(div).render(
11+
<React.StrictMode>
12+
<App />
13+
</React.StrictMode>
14+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
some text

webpack.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ async function (env, argv) {
8585
new WebpackBarPlugin(),
8686
...(env.analyze ? [new BundleAnalyzerPlugin()] : []),
8787
new MiniCssExtractPlugin(),
88-
new Webpack.DefinePlugin({
89-
RUN: () => true
90-
})
9188
]
9289
});
9390
}

0 commit comments

Comments
 (0)