Files
food-tracer/client/src/markdownPdf.ts
T

96 lines
4.4 KiB
TypeScript

import { fromMarkdown } from "mdast-util-from-markdown";
import type { BlockContent, DefinitionContent, ListItem, PhrasingContent, RootContent } from "mdast";
import type { Content, ContentText } from "pdfmake/interfaces";
import { applyLineBreaks } from "./markdownBreaks";
/** Barva odkazů a citací — stejná jako text vedle, jen tlumenější. */
const MUTED_COLOR = '#6b7280';
const LINK_COLOR = '#15803d';
const CODE_BACKGROUND = '#f3f4f6';
/** Formátování zděděné od nadřazených uzlů (tučně uvnitř odkazu, kurzíva uvnitř tučného…). */
type InlineStyle = Pick<ContentText, 'bold' | 'italics' | 'decoration' | 'background' | 'color' | 'link'>;
/**
* Inline obsah se skládá do plochého pole textů, každý s celým svým formátováním.
* pdfmake styl vnořeného pole `text` zahodí (bpampuch/pdfmake#1174) — tučné
* nebo kurzíva by se ztratily, jakmile je kolem ještě další úroveň, typicky
* v položce seznamu.
*/
const convertInline = (nodeList: PhrasingContent[], style: InlineStyle = {}): Content[] =>
nodeList.flatMap(node => convertPhrasing(node, style));
const convertPhrasing = (node: PhrasingContent, style: InlineStyle): Content[] => {
switch (node.type) {
case 'text':
return [{ ...style, text: node.value }];
case 'strong':
return convertInline(node.children, { ...style, bold: true });
case 'emphasis':
return convertInline(node.children, { ...style, italics: true });
case 'delete':
return convertInline(node.children, { ...style, decoration: 'lineThrough' });
case 'inlineCode':
return [{ ...style, text: node.value, background: CODE_BACKGROUND }];
case 'break':
return [{ ...style, text: '\n' }];
case 'link':
return convertInline(node.children, { ...style, link: node.url, color: LINK_COLOR, decoration: 'underline' });
// Obrázky z internetu se do PDF nestahují, zůstane popisek
case 'image':
return [{ ...style, text: node.alt ?? '' }];
case 'html':
return [{ ...style, text: node.value }];
default:
return 'children' in node ? convertInline(node.children as PhrasingContent[], style) : [];
}
};
/** Položka seznamu s jediným odstavcem je jen text, jinak se bloky skládají pod sebe. */
const convertListItem = (item: ListItem): Content => {
const [first, ...rest] = item.children;
if (first?.type === 'paragraph' && !rest.length) return { text: convertInline(first.children) };
return { stack: convertBlocks(item.children) };
};
const convertBlock = (node: RootContent | BlockContent | DefinitionContent): Content | undefined => {
switch (node.type) {
case 'heading':
return { text: convertInline(node.children), style: node.depth <= 2 ? 'noteHeading' : 'noteSubheading' };
case 'paragraph':
return { text: convertInline(node.children), style: 'noteParagraph' };
case 'list':
return node.ordered
? { ol: node.children.map(convertListItem), start: node.start ?? 1, style: 'noteList' }
: { ul: node.children.map(convertListItem), style: 'noteList' };
case 'blockquote':
return { stack: convertBlocks(node.children), italics: true, color: MUTED_COLOR, margin: [12, 0, 0, 6] };
case 'code':
return {
table: { widths: ['*'], body: [[{ text: node.value, fillColor: CODE_BACKGROUND, fontSize: 9 }]] },
layout: 'noBorders',
style: 'noteParagraph',
};
case 'thematicBreak':
return {
canvas: [{ type: 'line', x1: 0, y1: 0, x2: 515, y2: 0, lineWidth: 0.5, lineColor: '#d1d5db' }],
margin: [0, 4, 0, 10],
};
case 'html':
return { text: node.value, style: 'noteParagraph' };
default:
return undefined;
}
};
const convertBlocks = (nodeList: (RootContent | BlockContent | DefinitionContent)[]): Content[] =>
nodeList.map(convertBlock).filter((item): item is Content => item !== undefined);
/**
* Převede postup receptu z Markdownu na obsah pdfmake. Parser je stejný jako
* u zobrazení v aplikaci (react-markdown stojí na mdast), včetně zalomení řádků,
* takže PDF vypadá jako karta receptu.
*/
export const convertMarkdownToPdf = (markdown: string): Content[] =>
convertBlocks(applyLineBreaks(fromMarkdown(markdown)).children);