Skip to content

Commit

Permalink
fix: auto-code group transformation (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 committed Apr 2, 2024
1 parent a23bb16 commit 56ca0c4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/server/plugins/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,31 @@ function transformCodeGroups(currChildIdx: number, children: ContentNode[] = [])
return
}

const group: ContentNode[] = []
const group: {
idx: number
node: ContentNode
}[] = []

for (let i = currChildIdx; i < children.length; i++) {
const nextNode = children[i]
if (!_isNamedCodeBlock(nextNode)) {
break
}
group.push(nextNode)
children[i] = { type: 'text', value: '' }
group.push({ idx: i, node: nextNode })
}

// Replace current children with the new code group if it has two or more code blocks
if (group.length > 1) {
// Only reset children if we have more than one code block
// Code here is to avoid empty text nodes in the markdown AST
for (const { idx } of group) {
children[idx] = { type: 'text', value: '' }
}

children[currChildIdx] = {
type: 'element',
tag: 'CodeGroup',
children: [...group],
children: group.map((g) => g.node),
}
}
}
Expand Down

0 comments on commit 56ca0c4

Please sign in to comment.