Skip to content

Commit 7ee8f4c

Browse files
fix: parse hyphenated language names for code examples on package docs (#2734)
Co-authored-by: Willow (GHOST) <git@willow.sh>
1 parent ebdbf0c commit 7ee8f4c

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

server/utils/docs/render.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ async function renderJsDocTags(tags: JsDocTag[], symbolLookup: SymbolLookup): Pr
191191
: null
192192
const examplePromises = examples.map(async example => {
193193
if (!example.doc) return ''
194-
const langMatch = example.doc.match(/```(\w+)?/)
194+
const langMatch = example.doc.match(/```[ \t]*([-\w]+)?/)
195195
const lang = langMatch?.[1] || 'typescript'
196-
const code = example.doc.replace(/```\w*\n?/g, '').trim()
196+
const code = example.doc.replace(/```[ \t]*[-\w]*[ \t]*(?:\r\n|\r|\n)?/g, '').trim()
197197
return highlightCodeBlock(code, lang)
198198
})
199199

test/unit/server/utils/docs/render.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ function createClassSymbol(classDef: DenoDocNode['classDef']): MergedSymbol {
2121
}
2222
}
2323

24-
function createFunctionSymbol(name: string): MergedSymbol {
24+
function createFunctionSymbol(name: string, jsDoc?: DenoDocNode['jsDoc']): MergedSymbol {
2525
const node: DenoDocNode = {
2626
name,
2727
kind: 'function',
28+
jsDoc,
2829
functionDef: {
2930
params: [],
3031
returnType: { repr: 'void', kind: 'keyword', keyword: 'void' },
@@ -34,6 +35,7 @@ function createFunctionSymbol(name: string): MergedSymbol {
3435
return {
3536
name,
3637
kind: 'function',
38+
jsDoc,
3739
nodes: [node],
3840
}
3941
}
@@ -192,3 +194,25 @@ describe('renderDocNodes ordering', () => {
192194
expect(alphaIndex).toBeLessThan(betaIndex)
193195
})
194196
})
197+
198+
describe('renderDocNodes examples', () => {
199+
it('handles hyphenated fenced code languages in @example tags', async () => {
200+
const symbol = createFunctionSymbol('renderTemplate', {
201+
tags: [
202+
{
203+
kind: 'example',
204+
doc: '```glimmer-ts\nconst greeting = <template>Hello</template>\n```',
205+
},
206+
],
207+
})
208+
209+
const html = await renderDocNodes([symbol], new Map())
210+
211+
expect(html).toContain('<h4>Example</h4>')
212+
expect(html).toContain('shiki')
213+
expect(html).toContain('greeting')
214+
expect(html).not.toMatch(/(^|[>\s])-ts([<\s]|$)/)
215+
expect(html).not.toContain('-ts')
216+
expect(html).not.toContain('```')
217+
})
218+
})

0 commit comments

Comments
 (0)