mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
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
This commit is contained in:
@@ -463,6 +463,31 @@ describe('downloadTripPDF', () => {
|
||||
expect(html).not.toContain('<script>alert(1)</script>')
|
||||
})
|
||||
|
||||
// #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<script>alert(1)</script>' }],
|
||||
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('<script>alert(1)</script>')
|
||||
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 <b>B12</b>' }] })
|
||||
const html = getIframe()!.srcdoc
|
||||
|
||||
expect(html).toContain('<div class="note-time transport-note">Gate <b>B12</b></div>')
|
||||
// Markdown like the planner (#3236), raw HTML still printed as text.
|
||||
expect(html).toContain('<div class="note-time transport-note md"><p><bdi>Gate <b>B12</b></bdi></p></div>')
|
||||
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()
|
||||
|
||||
@@ -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
|
||||
? `<div class="empty-day">${escHtml(tr('dayplan.emptyDay'))}</div>`
|
||||
: `<ol class="timeline">${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() ? `<div class="day-intro">${escHtml(day.notes.trim())}</div>` : ''
|
||||
const intro = day.notes && day.notes.trim() ? `<div class="day-intro md">${markdown.html(day.notes.trim())}</div>` : ''
|
||||
const stays = staysBlock(staysForDay(day, days, accommodationList), day, ctx)
|
||||
|
||||
// A real <table> so the browser repeats the <thead> day header at the top of
|
||||
|
||||
@@ -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) =>
|
||||
`<div class="info-row"><span class="info-spacer"></span><div class="info-text muted md">${markdown.html(text)}</div></div>`,
|
||||
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; }
|
||||
|
||||
@@ -50,6 +50,7 @@ function context(over: Partial<PdfDayContext> = {}): PdfDayContext {
|
||||
placesById: new Map([[fullPlace.id, fullPlace]]),
|
||||
photoOf: () => null,
|
||||
markdown: (text) => `<p>${text}</p>`,
|
||||
markdownHtml: (text) => `<p>${text}</p>`,
|
||||
...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 <tickets>']);
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -30,8 +30,10 @@ export interface PdfDayContext {
|
||||
placesById: ReadonlyMap<number, Place>;
|
||||
/** 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 ? `<div class="note-time">${escHtml(location)}</div>` : ''}
|
||||
${extras.length ? `<div class="note-extra">${extras.join('')}</div>` : ''}
|
||||
${note ? `<div class="note-time transport-note">${escHtml(note)}</div>` : ''}
|
||||
${note ? `<div class="note-time transport-note md">${ctx.markdownHtml(note)}</div>` : ''}
|
||||
</div>
|
||||
</div>`;
|
||||
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 = `<div class="note-card${colour ? ' note-tinted' : ''}"${colour ? ` style="--note:${colour}"` : ''}>
|
||||
<span class="note-icon">${noteIcon(note.icon)}</span>
|
||||
<div class="note-body"><div class="note-text">${escHtml(note.text)}</div>${
|
||||
subtitle ? `<div class="note-time">${escHtml(subtitle)}</div>` : ''
|
||||
subtitle ? `<div class="note-time md">${ctx.markdownHtml(subtitle)}</div>` : ''
|
||||
}</div>
|
||||
</div>`;
|
||||
return item('note', timeColumn('', ''), '', card);
|
||||
|
||||
Reference in New Issue
Block a user