Skip to content

Commit c92ba3f

Browse files
committed
fix: handle h1 to h6 a special case when discovering components
1 parent 14418e7 commit c92ba3f

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"typescript": "^6.0.2"
5353
},
5454
"dependencies": {
55+
"@poppinss/string": "^1.7.1",
5556
"@shikijs/rehype": "^4.0.2",
5657
"@shikijs/transformers": "^4.0.2",
5758
"escape-goat": "^4.0.0",

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import { type VFile } from 'vfile'
1111
import { type Edge } from 'edge.js'
12+
import string from '@poppinss/string'
1213
import { htmlEscape } from 'escape-goat'
1314
import { find, html } from 'property-information'
1415
import { type VoidHtmlTags, voidHtmlTags } from 'html-tags'
@@ -111,8 +112,10 @@ export function discoverMarkdownComponents(edge: Edge, prefix: string) {
111112
/**
112113
* Collect components with the tagName.
113114
*/
114-
const tagName = componentName.replace(new RegExp(componentsBasePath), '').replace(/_/g, '-')
115-
result[tagName] = componentName
115+
const tagName = componentName.replace(new RegExp(componentsBasePath), '')
116+
const lowerTagName = tagName.toLowerCase()
117+
const isHeadingTag = /^h[1-6]$/.test(lowerTagName)
118+
result[isHeadingTag ? lowerTagName : string.dashCase(tagName)] = componentName
116119
return result
117120
}, {})
118121
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h2 class="custom-h5" id="{{ node.properties.id }}">
2+
@markdownSlot()
3+
</h2>
4+

tests/markdown.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ Here is a paragraph with a [link](./foo)\`
366366
assert.notMatch(result.content, /<h6(?! class="custom-h6")/)
367367
})
368368

369+
test('use h5 component saved as H5.edge to override h5 rendering', async ({ assert }) => {
370+
const edge = new Edge()
371+
edge.mount(join(import.meta.dirname, 'fixtures/views'))
372+
edge.use(edgeMarkdown, {})
373+
374+
const renderer = edge.share({})
375+
const result = await renderer.getState().$markdown.render({
376+
content: dedent`
377+
##### Section title
378+
`,
379+
})
380+
381+
assert.include(result.content, 'class="custom-h5"')
382+
assert.notMatch(result.content, /<h5(?! class="custom-h5")/)
383+
})
384+
369385
test('preview returns full content when there is no h2', async ({ assert }) => {
370386
const edge = new Edge()
371387
edge.mount(join(import.meta.dirname, 'fixtures/views'))

0 commit comments

Comments
 (0)