From addcbe4801397594dd3c75da7b7c600573068244 Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 27 Nov 2026 19:58:00 +0100 Subject: [PATCH] fix(pdf): render markdown in day notes, booking notes and the day intro The planner renders a day note's second line and a booking's notes as markdown, and place texts in the PDF already went through the app's sanitized markdown pipeline (#2692). Day notes, booking notes and the day intro were still printed through escHtml, so the export showed the raw syntax (asterisks, list dashes, rules) instead of the formatting. They now use the same sanitized pipeline as bare markup inside their own card, without the info row a place text gets, and drop the pre-line whitespace that would double every markdown line break. Raw HTML in a note is still printed as text. Closes #3236 --- client/src/components/PDF/TripPDF.test.ts | 30 ++++++++++++++++++-- client/src/components/PDF/TripPDF.tsx | 5 ++-- client/src/components/PDF/pdfMarkdown.ts | 16 ++++++++--- client/src/components/PDF/tripPdfDay.test.ts | 5 ++-- client/src/components/PDF/tripPdfDay.ts | 13 +++++---- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/client/src/components/PDF/TripPDF.test.ts b/client/src/components/PDF/TripPDF.test.ts index ef3154e75..c41c910b3 100644 --- a/client/src/components/PDF/TripPDF.test.ts +++ b/client/src/components/PDF/TripPDF.test.ts @@ -463,6 +463,31 @@ describe('downloadTripPDF', () => { expect(html).not.toContain('') }) + // #3236: day notes, transport notes and the day intro are markdown in the planner too. + it('FE-COMP-TRIPPDF-016m: renders markdown in day notes, transport notes and the day intro, sanitized', async () => { + const md = { + ...richArgs, + days: [{ ...dayWithPlaces, notes: '**Intro** day\n\n- museum\n- lunch' }], + dayNotes: [{ ...dayNote, time: '*Bring* water\n___\n' }], + reservations: [{ ...transportReservation, notes: 'Seat **14A**\n1. check in\n2. board' }], + } + await downloadTripPDF(md) + const html = getIframe()?.srcdoc ?? '' + const doc = new DOMParser().parseFromString(html, 'text/html') + const intro = doc.querySelector('.day-intro')! + expect(intro.querySelector('strong')?.textContent).toBe('Intro') + expect([...intro.querySelectorAll('li')].map(li => li.textContent)).toEqual(['museum', 'lunch']) + const noteLine = doc.querySelector('.tl-note .note-time')! + expect(noteLine.querySelector('em')?.textContent).toBe('Bring') + expect(noteLine.querySelector('hr')).not.toBeNull() + expect(noteLine.querySelector('script')).toBeNull() + expect(noteLine.textContent).toContain('') + const transportNote = doc.querySelector('.transport-note')! + expect(transportNote.querySelector('strong')?.textContent).toBe('14A') + expect([...transportNote.querySelectorAll('ol li')].map(li => li.textContent)).toEqual(['check in', 'board']) + expect(html).not.toContain('**') + }) + it('FE-COMP-TRIPPDF-016b: formats price chip and totals in the trip currency, not EUR', async () => { const usdArgs = { ...richArgs, @@ -1464,7 +1489,8 @@ describe('downloadTripPDF — transport notes', () => { await downloadTripPDF({ ...richArgs, reservations: [{ ...withNote, notes: 'Gate B12' }] }) const html = getIframe()!.srcdoc - expect(html).toContain('
Gate <b>B12</b>
') + // Markdown like the planner (#3236), raw HTML still printed as text. + expect(html).toContain('

Gate <b>B12</b>

') expect(html).not.toContain('pdf-no-transport-notes"') expect(notesToggle()!.getAttribute('aria-checked')).toBe('true') }) @@ -1472,7 +1498,7 @@ describe('downloadTripPDF — transport notes', () => { it('FE-PDF-TNOTES-002: without any transport note there is nothing to switch', async () => { await downloadTripPDF(richArgs) - expect(getIframe()!.srcdoc).not.toContain('class="note-time transport-note"') + expect(getIframe()!.srcdoc).not.toContain('class="note-time transport-note') expect(notesToggle()).toBeNull() // The page break switch is still there on its own. expect(document.querySelector('#pdf-daybreak-toggle')).not.toBeNull() diff --git a/client/src/components/PDF/TripPDF.tsx b/client/src/components/PDF/TripPDF.tsx index 9a2a7dab2..7eb89e3ab 100644 --- a/client/src/components/PDF/TripPDF.tsx +++ b/client/src/components/PDF/TripPDF.tsx @@ -245,6 +245,7 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor return url ? thumbs.get(url) ?? url : null }, markdown: text => markdown.row(text), + markdownHtml: text => markdown.html(text), } /* @@ -303,7 +304,7 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor ? `
${escHtml(tr('dayplan.emptyDay'))}
` : `
    ${merged.map(item => { if (item.type === 'transport') return bookingItem(item.data as PdfBooking, day, ctx, flags) - if (item.type === 'note') return noteItem(item.data as DayNote) + if (item.type === 'note') return noteItem(item.data as DayNote, ctx) const a = item.data as Assignment if (!a.place) return '' number++ @@ -316,7 +317,7 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor cost: dayCost(day.id) || null, }, ctx) const strip = photoStrip((stripPhotos.get(day.id) || []).map(p => ({ ...p, url: stripCopies.get(p.url) ?? p.url }))) - const intro = day.notes && day.notes.trim() ? `
    ${escHtml(day.notes.trim())}
    ` : '' + const intro = day.notes && day.notes.trim() ? `
    ${markdown.html(day.notes.trim())}
    ` : '' const stays = staysBlock(staysForDay(day, days, accommodationList), day, ctx) // A real so the browser repeats the day header at the top of diff --git a/client/src/components/PDF/pdfMarkdown.ts b/client/src/components/PDF/pdfMarkdown.ts index 1ee58778c..9e2ed9c39 100644 --- a/client/src/components/PDF/pdfMarkdown.ts +++ b/client/src/components/PDF/pdfMarkdown.ts @@ -1,15 +1,22 @@ import { createMarkdownHtml } from '../shared/markdownHtml'; /** - * A place's description and notes are markdown in the app, so the export - * renders them the same way instead of printing the raw syntax (#2692): one - * info row per text, the HTML from the app's own sanitized pipeline. + * Place descriptions and notes, day notes, booking notes and the day intro are + * markdown in the app, so the export renders them the same way instead of + * printing the raw syntax (#2692, #3236), the HTML from the app's own sanitized + * pipeline. `row` lays a place's text out as an info row, `html` is the bare + * markup for a card that brings its own box. */ -export function createPdfMarkdown(): { row: (text: string) => string; dispose: () => void } { +export function createPdfMarkdown(): { + row: (text: string) => string; + html: (text: string) => string; + dispose: () => void; +} { const markdown = createMarkdownHtml(); return { row: (text) => `
    ${markdown.html(text)}
    `, + html: markdown.html, dispose: markdown.dispose, }; } @@ -17,6 +24,7 @@ export function createPdfMarkdown(): { row: (text: string) => string; dispose: ( /** The markdown's own spacing, lists, tables and links at the export's small size. */ export const PDF_MARKDOWN_CSS = ` .info-text.md { min-width: 0; overflow-wrap: anywhere; } + .note-time.md, .day-intro.md { white-space: normal; } .md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; } .md p, .md ul, .md ol, .md table, .md blockquote, .md pre { margin: 0 0 4px; } diff --git a/client/src/components/PDF/tripPdfDay.test.ts b/client/src/components/PDF/tripPdfDay.test.ts index d84300aad..90563946c 100644 --- a/client/src/components/PDF/tripPdfDay.test.ts +++ b/client/src/components/PDF/tripPdfDay.test.ts @@ -50,6 +50,7 @@ function context(over: Partial = {}): PdfDayContext { placesById: new Map([[fullPlace.id, fullPlace]]), photoOf: () => null, markdown: (text) => `

    ${text}

    `, + markdownHtml: (text) => `

    ${text}

    `, ...over, }; } @@ -169,7 +170,7 @@ describe('noteItem', () => { icon: 'Ticket', color: '#2563eb', } as DayNote; - const tinted = parse(noteItem(note)); + const tinted = parse(noteItem(note, context())); expect(tinted.querySelector('.note-card')?.className).toBe('note-card note-tinted'); expect(tinted.querySelector('.note-card')?.getAttribute('style')).toBe('--note:#2563eb'); expect(texts(tinted, '.note-text')).toEqual(['Buy ']); @@ -177,7 +178,7 @@ describe('noteItem', () => { expect(texts(tinted, '.note-time')).toEqual(['08:00']); expect(tinted.querySelector('.tl-start')).toBeNull(); - const plain = parse(noteItem({ ...note, color: 'not-a-colour', time: null } as DayNote)); + const plain = parse(noteItem({ ...note, color: 'not-a-colour', time: null } as DayNote, context())); expect(plain.querySelector('.note-card')?.className).toBe('note-card'); expect(plain.querySelector('.note-time')).toBeNull(); }); diff --git a/client/src/components/PDF/tripPdfDay.ts b/client/src/components/PDF/tripPdfDay.ts index fcc7999b8..ed432068a 100644 --- a/client/src/components/PDF/tripPdfDay.ts +++ b/client/src/components/PDF/tripPdfDay.ts @@ -30,8 +30,10 @@ export interface PdfDayContext { placesById: ReadonlyMap; /** The picture to print for a place, already shrunk where it could be; null for none. */ photoOf: (place: Assignment['place']) => string | null; - /** A place's description or notes as sanitized markdown. */ + /** A place's description or notes as sanitized markdown, laid out as an info row. */ markdown: (text: string) => string; + /** A note's markdown as sanitized HTML, for a card that brings its own box. */ + markdownHtml: (text: string) => string; } const NEUTRAL = '#6b6b75'; @@ -214,7 +216,7 @@ export function bookingItem(r: PdfBooking, day: Day, ctx: PdfDayContext, flags: ${bookingLines(r, tr).map(bookingLineHtml).join('')} ${location ? `
    ${escHtml(location)}
    ` : ''} ${extras.length ? `
    ${extras.join('')}
    ` : ''} - ${note ? `
    ${escHtml(note)}
    ` : ''} + ${note ? `
    ${ctx.markdownHtml(note)}
    ` : ''} `; return item('booking', timeColumn(start, end), bookingIcon(r.type, 11), card, `--kind:${tint}`); @@ -226,15 +228,16 @@ export function bookingItem(r: PdfBooking, day: Day, ctx: PdfDayContext, flags: * A day note. A coloured note carries its colour as the rule along its edge and its * icon (#1629), not as a fill: a tint costs ink and reads as grey on a mono printer. */ -export function noteItem(note: DayNote): string { +export function noteItem(note: DayNote, ctx: PdfDayContext): string { const colour = note.color ? hexColour(note.color, '') : ''; // A note's `time` is the free line the day plan prints under its text, not a clock, - // so it stays in the card and the time column stays empty. + // so it stays in the card and the time column stays empty. The planner renders it as + // markdown, so the card does too (#3236). const subtitle = typeof note.time === 'string' ? note.time.trim() : ''; const card = `
    ${noteIcon(note.icon)}
    ${escHtml(note.text)}
    ${ - subtitle ? `
    ${escHtml(subtitle)}
    ` : '' + subtitle ? `
    ${ctx.markdownHtml(subtitle)}
    ` : '' }
    `; return item('note', timeColumn('', ''), '', card);