Skip to content

Commit 9c69dfd

Browse files
lex111yangshun
authored andcommitted
feat(v2): allow line highlighting (#1860)
* feat(v2): allow line highlighting * Refactor: use parse-numeric-range for parsing * Add line highlighting for live code blocks * feat(v2): add sticky footer (#1855) * feat(v2): add sticky footer * Update CHANGELOG-2.x.md * Update CHANGELOG-2.x.md * fix(v2): remove empty doc sidebar container (#1870) * docs: showcase user Amphora (#1873) * Add Amphora Data link to users Adds Amphora Data to the list of users * Add Amphora Data logo * fix case of image path * Move Image into users directory * use black amphora image * fix(v2): fix search input blur on desktop (#1874) * docs(v2): showcase user mbt-bundle (#1875) * feat(v2): postcss should only use stage 3 features instead of stage 2 (#1872) * feat(v2): add ability expand all items in doc sidebar (#1876) * feat(v2): add ability expand all items in doc sidebar * Fix tests * Refactor: use themeConfig * Improve docs * Revert unnecessary changes * Refactor: better consistency * Revert extra change * Refactor: use useDocusaurusContext to get config value * chore(v2): update changelog * chore(v2): update showcase and broken link * perf(v2): disable hash for css modules localident in dev (#1882) * perf(v2): disable hash for css modules localident in dev * changelog * feat(v2): display footer in docs page for consistency (#1883) * feat(v2): display footer in docs page * nits * address review * nits * docs(v2): fix format inline code (#1888) * docs(v2): add docs on useful client api (#1890) * docs(v2): add docs on useful client api * Update docusaurus-core.md * Update website/docs/docusaurus-core.md * Update website/docs/docusaurus-core.md * Update website/docs/docusaurus-core.md * Update website/docs/docusaurus-core.md * docs(v2): update config docs (#1885) * fix(v2): do not show categories with empty items (#1891) * styles(v2): update infima and fix styles (#1892) * fix(v2): wrong css class * v2.0.0-alpha.31 * chore(v2): update docs and changelog * docs(v2): update plugins, presets and themes docs (#1889) * docs(v2): update plugins, presets and themes docs * ideal image plugin * proof reading * Merge master * refactor(v2): Convert sitemap plugin to TypeScript (#1894) * Convert sitemap plugin to TypeScript Test - enabled the sitemap plugin in the v2 website and verified that the sitemap is created after running `docusaurus build`. * Addressing review comments * perf(v2): significantly reduce bundle size & initial html payload (#1898) * perf(v2): reduce bundle size significantly with super short chunk name and registry * changelog * use hash:8 as id for better long term caching * even shorter filename. slice contenthash * fix(v2): align search icon on small width device (#1893) * fix(v2): align search icon on small width device * nits * nits * refactor(v2): refactor dark toggle into a hook (#1899) * change(v2): refactor dark toggle into a theme * follow how themes resolve files * let theme hook to pick up default theme by itself * perf(v2): reduce memory usage consumption (#1900) * misc(v1): use primary color for hovered items in table of contents (#1871) * fix issue#1752 when element in side nav is hovered over the color changes. * Update main.css * fix(v1): mobile safari search input misalignment in header (#1895) * misc(v2): v1 backward compatibility for USE_SSH env var (#1880) * misc(v2): address comments * misc(v2): update CHANGELOG
1 parent 812a30b commit 9c69dfd

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

CHANGELOG-2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name.
99
- Refactor dark toggle into a hook.
1010
- Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1.
11+
- Add highlight specific lines in code blocks.
1112

1213
## 2.0.0-alpha.31
1314

packages/docusaurus-theme-classic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"classnames": "^2.2.6",
1414
"clipboard": "^2.0.4",
1515
"infima": "0.2.0-alpha.3",
16+
"parse-numeric-range": "^0.0.2",
1617
"prism-react-renderer": "^1.0.2",
1718
"react-toggle": "^4.1.1"
1819
},

packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import classnames from 'classnames';
1010
import Highlight, {defaultProps} from 'prism-react-renderer';
1111
import defaultTheme from 'prism-react-renderer/themes/palenight';
1212
import Clipboard from 'clipboard';
13+
import rangeParser from 'parse-numeric-range';
1314
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
1415
import styles from './styles.module.css';
1516

16-
export default ({children, className: languageClassName}) => {
17+
const highlightLinesRangeRegex = /{([\d,-]+)}/;
18+
19+
export default ({children, className: languageClassName, metastring}) => {
1720
const {
1821
siteConfig: {
1922
themeConfig: {prismTheme},
@@ -22,7 +25,12 @@ export default ({children, className: languageClassName}) => {
2225
const [showCopied, setShowCopied] = useState(false);
2326
const target = useRef(null);
2427
const button = useRef(null);
28+
let highlightLines = [];
2529

30+
if (metastring && highlightLinesRangeRegex.test(metastring)) {
31+
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
32+
highlightLines = rangeParser.parse(highlightLinesRange).filter(n => n > 0);
33+
}
2634
useEffect(() => {
2735
let clipboard;
2836

@@ -61,13 +69,21 @@ export default ({children, className: languageClassName}) => {
6169
ref={target}
6270
className={classnames(className, styles.codeBlock)}
6371
style={style}>
64-
{tokens.map((line, i) => (
65-
<div key={i} {...getLineProps({line, key: i})}>
66-
{line.map((token, key) => (
67-
<span key={key} {...getTokenProps({token, key})} />
68-
))}
69-
</div>
70-
))}
72+
{tokens.map((line, i) => {
73+
const lineProps = getLineProps({line, key: i});
74+
75+
if (highlightLines.includes(i + 1)) {
76+
lineProps.className = `${lineProps.className} highlight-line`;
77+
}
78+
79+
return (
80+
<div key={i} {...lineProps}>
81+
{line.map((token, key) => (
82+
<span key={key} {...getTokenProps({token, key})} />
83+
))}
84+
</div>
85+
);
86+
})}
7187
</pre>
7288
<button
7389
ref={button}

packages/docusaurus-theme-live-codeblock/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@philpl/buble": "^0.19.7",
1212
"classnames": "^2.2.6",
1313
"clipboard": "^2.0.4",
14+
"parse-numeric-range": "^0.0.2",
1415
"prism-react-renderer": "^1.0.2",
1516
"react-live": "^2.2.1"
1617
},

packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ import classnames from 'classnames';
1010
import Highlight, {defaultProps} from 'prism-react-renderer';
1111
import defaultTheme from 'prism-react-renderer/themes/palenight';
1212
import Clipboard from 'clipboard';
13+
import rangeParser from 'parse-numeric-range';
1314
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
1415
import Playground from '@theme/Playground';
1516
import styles from './styles.module.css';
1617

17-
export default ({children, className: languageClassName, live, ...props}) => {
18+
const highlightLinesRangeRegex = /{([\d,-]+)}/;
19+
20+
export default ({
21+
children,
22+
className: languageClassName,
23+
live,
24+
metastring,
25+
...props
26+
}) => {
1827
const {
1928
siteConfig: {
2029
themeConfig: {prismTheme},
@@ -23,6 +32,12 @@ export default ({children, className: languageClassName, live, ...props}) => {
2332
const [showCopied, setShowCopied] = useState(false);
2433
const target = useRef(null);
2534
const button = useRef(null);
35+
let highlightLines = [];
36+
37+
if (metastring && highlightLinesRangeRegex.test(metastring)) {
38+
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
39+
highlightLines = rangeParser.parse(highlightLinesRange).filter(n => n > 0);
40+
}
2641

2742
useEffect(() => {
2843
let clipboard;
@@ -73,13 +88,21 @@ export default ({children, className: languageClassName, live, ...props}) => {
7388
ref={target}
7489
className={classnames(className, styles.codeBlock)}
7590
style={style}>
76-
{tokens.map((line, i) => (
77-
<div key={i} {...getLineProps({line, key: i})}>
78-
{line.map((token, key) => (
79-
<span key={key} {...getTokenProps({token, key})} />
80-
))}
81-
</div>
82-
))}
91+
{tokens.map((line, i) => {
92+
const lineProps = getLineProps({line, key: i});
93+
94+
if (highlightLines.includes(i + 1)) {
95+
lineProps.className = `${lineProps.className} highlight-line`;
96+
}
97+
98+
return (
99+
<div key={i} {...lineProps}>
100+
{line.map((token, key) => (
101+
<span key={key} {...getTokenProps({token, key})} />
102+
))}
103+
</div>
104+
);
105+
})}
83106
</pre>
84107
<button
85108
ref={button}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11842,6 +11842,11 @@ parse-json@^4.0.0:
1184211842
error-ex "^1.3.1"
1184311843
json-parse-better-errors "^1.0.1"
1184411844

11845+
parse-numeric-range@^0.0.2:
11846+
version "0.0.2"
11847+
resolved "https://registry.yarnpkg.com/parse-numeric-range/-/parse-numeric-range-0.0.2.tgz#b4f09d413c7adbcd987f6e9233c7b4b210c938e4"
11848+
integrity sha1-tPCdQTx6282Yf26SM8e0shDJOOQ=
11849+
1184511850
parse-path@^4.0.0:
1184611851
version "4.0.1"
1184711852
resolved "https://registry.yarnpkg.com/parse-path/-/parse-path-4.0.1.tgz#0ec769704949778cb3b8eda5e994c32073a1adff"

0 commit comments

Comments
 (0)