|
|
@@ -7,6 +7,9 @@
|
|
|
* Set `showLineNumbers` to `true` to always display line number
|
|
|
* @property {boolean} [ignoreMissing]
|
|
|
* Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
|
|
|
+ * @property {string} [defaultLanguage]
|
|
|
+ * Uses the specified language as the default if none is specified. Takes precedence over `ignoreMissing`.
|
|
|
+ * Note: The language must be registered with refractor.
|
|
|
*/
|
|
|
|
|
|
import { visit } from 'unist-util-visit'
|
|
|
@@ -188,7 +191,7 @@ const rehypePrismGenerator = (refractor) => {
|
|
|
node.properties.className = []
|
|
|
}
|
|
|
node.properties.className.push('code-highlight')
|
|
|
- const lang = getLanguage(node)
|
|
|
+ const lang = getLanguage(node) || options.defaultLanguage
|
|
|
|
|
|
/** @type {Element} */
|
|
|
let refractorRoot
|
|
|
@@ -197,10 +200,10 @@ const rehypePrismGenerator = (refractor) => {
|
|
|
if (lang) {
|
|
|
try {
|
|
|
let rootLang
|
|
|
- if (lang?.includes('diff-')){
|
|
|
- rootLang=lang.split('-')[1]
|
|
|
- } else{
|
|
|
- rootLang=lang
|
|
|
+ if (lang?.includes('diff-')) {
|
|
|
+ rootLang = lang.split('-')[1]
|
|
|
+ } else {
|
|
|
+ rootLang = lang
|
|
|
}
|
|
|
// @ts-ignore
|
|
|
refractorRoot = refractor.highlight(toString(node), rootLang)
|
|
|
@@ -273,9 +276,15 @@ const rehypePrismGenerator = (refractor) => {
|
|
|
}
|
|
|
|
|
|
// Diff classes
|
|
|
- if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '-') {
|
|
|
+ if (
|
|
|
+ (lang === 'diff' || lang?.includes('diff-')) &&
|
|
|
+ toString(line).substring(0, 1) === '-'
|
|
|
+ ) {
|
|
|
line.properties.className.push('deleted')
|
|
|
- } else if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '+') {
|
|
|
+ } else if (
|
|
|
+ (lang === 'diff' || lang?.includes('diff-')) &&
|
|
|
+ toString(line).substring(0, 1) === '+'
|
|
|
+ ) {
|
|
|
line.properties.className.push('inserted')
|
|
|
}
|
|
|
}
|