diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index db9ce20328bd..e8139d26401a 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -8,6 +8,7 @@ - Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name. - Refactor dark toggle into a hook. - Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1. +- Add highlight specific lines in code blocks. ## 2.0.0-alpha.31 diff --git a/packages/docusaurus-theme-classic/package.json b/packages/docusaurus-theme-classic/package.json index bdac8087f0b8..1d41e20657db 100644 --- a/packages/docusaurus-theme-classic/package.json +++ b/packages/docusaurus-theme-classic/package.json @@ -13,6 +13,7 @@ "classnames": "^2.2.6", "clipboard": "^2.0.4", "infima": "0.2.0-alpha.3", + "parse-numeric-range": "^0.0.2", "prism-react-renderer": "^1.0.2", "react-toggle": "^4.1.1" }, diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js index 1914e65b7856..1703b57b484c 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js @@ -10,10 +10,13 @@ import classnames from 'classnames'; import Highlight, {defaultProps} from 'prism-react-renderer'; import defaultTheme from 'prism-react-renderer/themes/palenight'; import Clipboard from 'clipboard'; +import rangeParser from 'parse-numeric-range'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import styles from './styles.module.css'; -export default ({children, className: languageClassName}) => { +const highlightLinesRangeRegex = /{([\d,-]+)}/; + +export default ({children, className: languageClassName, metastring}) => { const { siteConfig: { themeConfig: {prismTheme}, @@ -22,7 +25,12 @@ export default ({children, className: languageClassName}) => { const [showCopied, setShowCopied] = useState(false); const target = useRef(null); const button = useRef(null); + let highlightLines = []; + if (metastring && highlightLinesRangeRegex.test(metastring)) { + const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1]; + highlightLines = rangeParser.parse(highlightLinesRange).filter(n => n > 0); + } useEffect(() => { let clipboard; @@ -61,13 +69,21 @@ export default ({children, className: languageClassName}) => { ref={target} className={classnames(className, styles.codeBlock)} style={style}> - {tokens.map((line, i) => ( -