Skip to content

Commit

Permalink
fix: remove empty style (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 6, 2023
1 parent a9a6ee7 commit 7fd2092
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/shikiji/src/core/renderer-hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ export function tokensToHast(
let col = 0

for (const token of line) {
const styles = [token.htmlStyle || stringifyTokenStyle(getTokenStyles(token))]

let tokenNode: Element = {
type: 'element',
tagName: 'span',
properties: {
style: styles.join(';'),
},
properties: {},
children: [{ type: 'text', value: token.content }],
}

const style = token.htmlStyle || stringifyTokenStyle(getTokenStyles(token))
if (style)
tokenNode.properties.style = style

tokenNode = options.transforms?.token?.(tokenNode, idx + 1, col, lineNode) || tokenNode

lineNode.children.push(tokenNode)
Expand Down
12 changes: 12 additions & 0 deletions packages/shikiji/test/themes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ function toggleTheme() {
expect(code2)
.toContain('font-style:inherit;--shiki-light-font-style:italic')
})

it('should not have empty style', async () => {
const input = 'This is plain text'
const code = await codeToHtml(input, {
lang: 'plaintext',
themes: {
light: 'material-theme-palenight',
dark: 'nord',
},
})
expect(code).not.toContain('style=""')
})
})

describe('codeToTokensWithThemes', () => {
Expand Down

0 comments on commit 7fd2092

Please sign in to comment.