Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): allow line highlighting #1860

Merged
merged 33 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8657816
feat(v2): allow line highlighting
lex111 Oct 19, 2019
a200e47
Refactor: use parse-numeric-range for parsing
lex111 Oct 22, 2019
a2947a4
Add line highlighting for live code blocks
lex111 Oct 22, 2019
2b0cc9b
Merge branch 'master' into lh
lex111 Oct 22, 2019
68d5adf
feat(v2): add sticky footer (#1855)
lex111 Oct 23, 2019
11f9977
fix(v2): remove empty doc sidebar container (#1870)
lex111 Oct 23, 2019
262dca3
docs: showcase user Amphora (#1873)
xtellurian Oct 24, 2019
0e66c90
fix(v2): fix search input blur on desktop (#1874)
lex111 Oct 24, 2019
91703eb
docs(v2): showcase user mbt-bundle (#1875)
tienvx Oct 24, 2019
22fb869
feat(v2): postcss should only use stage 3 features instead of stage 2…
endiliey Oct 24, 2019
ce6a725
feat(v2): add ability expand all items in doc sidebar (#1876)
lex111 Oct 24, 2019
3acf880
chore(v2): update changelog
endiliey Oct 24, 2019
6afe6b3
chore(v2): update showcase and broken link
endiliey Oct 25, 2019
8bcb5dc
perf(v2): disable hash for css modules localident in dev (#1882)
endiliey Oct 25, 2019
b347968
feat(v2): display footer in docs page for consistency (#1883)
endiliey Oct 25, 2019
11548da
docs(v2): fix format inline code (#1888)
lex111 Oct 25, 2019
a149f77
docs(v2): add docs on useful client api (#1890)
endiliey Oct 25, 2019
a9f752c
docs(v2): update config docs (#1885)
endiliey Oct 25, 2019
60a4d1d
fix(v2): do not show categories with empty items (#1891)
lex111 Oct 26, 2019
9fdda55
styles(v2): update infima and fix styles (#1892)
endiliey Oct 26, 2019
3cc1330
fix(v2): wrong css class
endiliey Oct 26, 2019
d4342dc
v2.0.0-alpha.31
endiliey Oct 26, 2019
242c9e0
chore(v2): update docs and changelog
endiliey Oct 26, 2019
2bbfbf8
docs(v2): update plugins, presets and themes docs (#1889)
endiliey Oct 26, 2019
c23f981
refactor(v2): Convert sitemap plugin to TypeScript (#1894)
moozzyk Oct 27, 2019
fabaf77
perf(v2): significantly reduce bundle size & initial html payload (#1…
endiliey Oct 27, 2019
84462e2
fix(v2): align search icon on small width device (#1893)
endiliey Oct 27, 2019
01f4d9a
refactor(v2): refactor dark toggle into a hook (#1899)
wgao19 Oct 27, 2019
98679b5
perf(v2): reduce memory usage consumption (#1900)
endiliey Oct 27, 2019
f14b6ee
misc(v1): use primary color for hovered items in table of contents (#…
blitz137 Oct 27, 2019
421598e
fix(v1): mobile safari search input misalignment in header (#1895)
jsardev Oct 27, 2019
812a30b
misc(v2): v1 backward compatibility for USE_SSH env var (#1880)
yns88 Oct 27, 2019
e509311
Merge branch 'lh' of https://github.com/lex111/docusaurus into lex111-lh
yangshun Oct 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix `swizzle` command not being able to swizzle single js file.
- Fix logo URL in footer to be appended with baseUrl automatically.
- Add the option `--no-open` for `start` command.
- Add highlight specific lines in code blocks.

## 2.0.0-alpha.27

Expand Down
47 changes: 39 additions & 8 deletions packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ import Clipboard from 'clipboard';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import styles from './styles.module.css';

export default ({children, className: languageClassName}) => {
const regexHighlightRange = /{([\d,-]+)}/;
lex111 marked this conversation as resolved.
Show resolved Hide resolved

const calculateLinesToHighlight = meta => {
if (!regexHighlightRange.test(meta)) {
return () => false;
}

const lineNumbers = regexHighlightRange
.exec(meta)[1]
.split(',')
.map(v => v.split('-').map(ve => parseInt(ve, 10)));

return index => {
const lineNumber = index + 1;
const inRange = lineNumbers.some(([start, end]) =>
end ? lineNumber >= start && lineNumber <= end : lineNumber === start,
);

return inRange;
};
};

export default ({children, className: languageClassName, metastring}) => {
const {
siteConfig: {
themeConfig: {prismTheme},
Expand All @@ -22,6 +44,7 @@ export default ({children, className: languageClassName}) => {
const [showCopied, setShowCopied] = useState(false);
const target = useRef(null);
const button = useRef(null);
const shouldHighlightLine = calculateLinesToHighlight(metastring);

useEffect(() => {
let clipboard;
Expand Down Expand Up @@ -61,13 +84,21 @@ export default ({children, className: languageClassName}) => {
ref={target}
className={classnames(className, styles.codeBlock)}
style={style}>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({line, key: i})}>
{line.map((token, key) => (
<span key={key} {...getTokenProps({token, key})} />
))}
</div>
))}
{tokens.map((line, i) => {
const lineProps = getLineProps({line, key: i});

if (shouldHighlightLine(i)) {
lineProps.className = `${lineProps.className} highlight-line`;
}

return (
<div key={i} {...lineProps}>
{line.map((token, key) => (
<span key={key} {...getTokenProps({token, key})} />
))}
</div>
);
})}
</pre>
<button
ref={button}
Expand Down