Skip to content

Commit 16d738d

Browse files
authored
fix: allow to override h1 to h6 through discovered components
fix: allow to override h1 to h6 through discovered components
2 parents 057db92 + c92ba3f commit 16d738d

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ export function discoverMarkdownComponents(edge: Edge, prefix: string) {
113113
* Collect components with the tagName.
114114
*/
115115
const tagName = componentName.replace(new RegExp(componentsBasePath), '')
116-
result[string.dashCase(tagName)] = componentName
116+
const lowerTagName = tagName.toLowerCase()
117+
const isHeadingTag = /^h[1-6]$/.test(lowerTagName)
118+
result[isHeadingTag ? lowerTagName : string.dashCase(tagName)] = componentName
117119
return result
118120
}, {})
119121
}
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+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h2 class="custom-h6" id="{{ node.properties.id }}">
2+
@markdownSlot()
3+
</h2>
4+

tests/markdown.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,38 @@ Here is a paragraph with a [link](./foo)\`
350350
assert.notInclude(result.content, 'Section content')
351351
})
352352

353+
test('use h6 component to override h6 rendering', async ({ assert }) => {
354+
const edge = new Edge()
355+
edge.mount(join(import.meta.dirname, 'fixtures/views'))
356+
edge.use(edgeMarkdown, {})
357+
358+
const renderer = edge.share({})
359+
const result = await renderer.getState().$markdown.render({
360+
content: dedent`
361+
###### Section title
362+
`,
363+
})
364+
365+
assert.include(result.content, 'class="custom-h6"')
366+
assert.notMatch(result.content, /<h6(?! class="custom-h6")/)
367+
})
368+
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+
353385
test('preview returns full content when there is no h2', async ({ assert }) => {
354386
const edge = new Edge()
355387
edge.mount(join(import.meta.dirname, 'fixtures/views'))

0 commit comments

Comments
 (0)