Skip to content

Commit cd0e02b

Browse files
committed
chore: improve package description rendering in markdown text
1 parent 123f321 commit cd0e02b

2 files changed

Lines changed: 35 additions & 24 deletions

File tree

app/components/MarkdownText.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ function stripAndEscapeHtml(text: string): string {
2626
// First decode any HTML entities in the input
2727
let stripped = decodeHtmlEntities(text)
2828
29+
// Check if original text has HTML tags or markdown images BEFORE stripping
30+
// Only strip package name for these "badge-style" descriptions
31+
const hasHtmlTags = /<\/?[a-z][^>]*>/i.test(stripped)
32+
const hasMarkdownImages = /!\[[^\]]*\]\([^)]*\)/.test(stripped)
33+
2934
// Then strip markdown image badges
3035
stripped = stripMarkdownImages(stripped)
3136
3237
// Then strip actual HTML tags (keep their text content)
3338
// Only match tags that start with a letter or / (to avoid matching things like "a < b > c")
3439
stripped = stripped.replace(/<\/?[a-z][^>]*>/gi, '')
3540
36-
if (props.packageName) {
41+
// Only strip package name if original text had HTML tags or markdown images
42+
// Normal descriptions like "Nuxt is a framework..." should keep the package name
43+
if ((hasHtmlTags || hasMarkdownImages) && props.packageName) {
3744
// Trim first to handle leading/trailing whitespace from stripped HTML
3845
stripped = stripped.trim()
3946
// Collapse multiple whitespace into single space

test/nuxt/components/MarkdownText.spec.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -259,91 +259,95 @@ describe('MarkdownText', () => {
259259
})
260260

261261
describe('packageName prop', () => {
262-
it('strips package name from the beginning of plain text', async () => {
262+
// Package name stripping ONLY happens for descriptions with HTML tags or markdown images
263+
// Normal plain text descriptions should keep the package name (like "Nuxt is a framework...")
264+
265+
it('does NOT strip package name from plain text descriptions', async () => {
266+
// Plain text descriptions should be kept as-is
263267
const component = await mountSuspended(MarkdownText, {
264268
props: {
265269
text: 'my-package - A great library',
266270
packageName: 'my-package',
267271
},
268272
})
269-
expect(component.text()).toBe('A great library')
273+
expect(component.text()).toBe('my-package - A great library')
270274
})
271275

272-
it('strips package name with colon separator', async () => {
276+
it('does NOT strip package name from plain text with colon', async () => {
273277
const component = await mountSuspended(MarkdownText, {
274278
props: {
275279
text: 'my-package: A great library',
276280
packageName: 'my-package',
277281
},
278282
})
279-
expect(component.text()).toBe('A great library')
283+
expect(component.text()).toBe('my-package: A great library')
280284
})
281285

282-
it('strips package name with em dash separator', async () => {
286+
it('strips package name from HTML-containing descriptions', async () => {
283287
const component = await mountSuspended(MarkdownText, {
284288
props: {
285-
text: 'my-package A great library',
289+
text: '<b>my-package</b> - A great library',
286290
packageName: 'my-package',
287291
},
288292
})
289293
expect(component.text()).toBe('A great library')
290294
})
291295

292-
it('strips package name without separator', async () => {
296+
it('strips package name from HTML descriptions with colon separator', async () => {
293297
const component = await mountSuspended(MarkdownText, {
294298
props: {
295-
text: 'my-package A great library',
299+
text: '<div>my-package: A great library</div>',
296300
packageName: 'my-package',
297301
},
298302
})
299303
expect(component.text()).toBe('A great library')
300304
})
301305

302-
it('is case-insensitive', async () => {
306+
it('strips package name from HTML descriptions with em dash', async () => {
303307
const component = await mountSuspended(MarkdownText, {
304308
props: {
305-
text: 'MY-PACKAGE - A great library',
309+
text: '<span>my-package — A great library</span>',
306310
packageName: 'my-package',
307311
},
308312
})
309313
expect(component.text()).toBe('A great library')
310314
})
311315

312-
it('does not strip package name from middle of text', async () => {
316+
it('is case-insensitive for HTML descriptions', async () => {
313317
const component = await mountSuspended(MarkdownText, {
314318
props: {
315-
text: 'A great my-package library',
319+
text: '<b>MY-PACKAGE</b> - A great library',
316320
packageName: 'my-package',
317321
},
318322
})
319-
expect(component.text()).toBe('A great my-package library')
323+
expect(component.text()).toBe('A great library')
320324
})
321325

322-
it('handles scoped package names', async () => {
326+
it('does not strip package name from middle of HTML text', async () => {
323327
const component = await mountSuspended(MarkdownText, {
324328
props: {
325-
text: '@org/my-package - A great library',
326-
packageName: '@org/my-package',
329+
text: '<div>A great my-package library</div>',
330+
packageName: 'my-package',
327331
},
328332
})
329-
expect(component.text()).toBe('A great library')
333+
expect(component.text()).toBe('A great my-package library')
330334
})
331335

332-
it('handles package names with special regex characters', async () => {
336+
it('handles scoped package names in HTML descriptions', async () => {
333337
const component = await mountSuspended(MarkdownText, {
334338
props: {
335-
text: 'pkg.name+test - A great library',
336-
packageName: 'pkg.name+test',
339+
text: '<b>@org/my-package</b> - A great library',
340+
packageName: '@org/my-package',
337341
},
338342
})
339343
expect(component.text()).toBe('A great library')
340344
})
341345

342-
it('strips package name from HTML-containing descriptions', async () => {
346+
it('handles package names with special regex characters in HTML', async () => {
343347
const component = await mountSuspended(MarkdownText, {
344348
props: {
345-
text: '<b>my-package</b> - A great library',
346-
packageName: 'my-package',
349+
text: '<b>pkg.name+test</b> - A great library',
350+
packageName: 'pkg.name+test',
347351
},
348352
})
349353
expect(component.text()).toBe('A great library')

0 commit comments

Comments
 (0)