Skip to content

Commit

Permalink
Fix toggling for code fences (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
swaldmann authored Jan 11, 2024
1 parent a174e45 commit a05fd01
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions .vitepress/lib/md-attrs-propagate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function install(md: MarkdownRenderer, classRegex=/impl (node|java)/) {
})
})

// intercept all container open renderes, like `container_tip_open`
// intercept all container open renderers, like `container_tip_open`
// because these add additional divs, which need to be instrumented as well
const { rules } = md.renderer
// console.log('rules', rules)
Expand All @@ -60,13 +60,20 @@ export function install(md: MarkdownRenderer, classRegex=/impl (node|java)/) {
const [tokens, idx] = args
const token = tokens[idx]
if (token.meta?.classes) {
const classes = token.meta?.classes as string
// delete classes in token.attrs as these would be rendered in addition,
// leading to 'Duplicate attribute' errors
deleteAttr('class', token.attrs)

let result:string = original(...args)
if (!classes.split(' ').some(cls => result.includes(cls))) { // some of classes already set?
const hasClass = (classes: any, result: any) => {
const match = result.match(/class="([^"]*)"/)
if (!match || match.length < 2) return false
const existing = match[1].split(/\s+/)
return classes.split(' ').some((cls: any) => existing.includes(cls))
}

// Usage in your code
if (!hasClass(token.meta.classes, result)) {
if (result.includes(' class="')) { // `class` attribute existing -> augment
result = result.replace(' class="', ` class="${token.meta.classes} `)
} else { // no `class` attribute -> set one
Expand Down

0 comments on commit a05fd01

Please sign in to comment.