refactor: use hast transform instead of regex for filling lines

pull/3398/head
Divyansh Singh 6 months ago
parent 563020ba61
commit 1c9f2aa8e4

@ -147,10 +147,6 @@ export async function highlight(
return s
}
const fillEmptyLines = (s: string) => {
return s.replace(/(<span class="line[^>]*>)(<\/span>)/g, '$1<wbr>$2')
}
str = removeMustache(str).trimEnd()
const highlighted = highlighter.codeToHtml(str, {
@ -164,6 +160,29 @@ export async function highlight(
if (vPre) node.properties['v-pre'] = ''
}
},
{
name: 'vitepress:empty-line',
pre(hast) {
hast.children.forEach((code) => {
if (code.type === 'element' && code.tagName === 'code') {
code.children.forEach((span) => {
if (
span.type === 'element' &&
span.tagName === 'span' &&
span.children.length === 0
) {
span.children.push({
type: 'element',
tagName: 'wbr',
properties: {},
children: []
})
}
})
}
})
}
},
...userTransformers
],
meta: { __raw: attrs },
@ -172,6 +191,6 @@ export async function highlight(
: { theme })
})
return fillEmptyLines(restoreMustache(highlighted))
return restoreMustache(highlighted)
}
}

Loading…
Cancel
Save