feat: export + detail receptů

feat: losování jídel
This commit is contained in:
Ondřej Anděl committed 2026-10-16 12:55:32 +02:00
1 parent 35cbc5a9ca
commit caffb6d8b4
23 files changed
+2674 -43

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
import { fromMarkdown } from "mdast-util-from-markdown";
import type { BlockContent, DefinitionContent, ListItem, PhrasingContent, RootContent } from "mdast";
import type { Content } 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';
const convertInline = (nodeList: PhrasingContent[]): Content[] => nodeList.map(convertPhrasing);
const convertPhrasing = (node: PhrasingContent): Content => {
switch (node.type) {
case 'text':
return node.value;
case 'strong':
return { text: convertInline(node.children), bold: true };
case 'emphasis':
return { text: convertInline(node.children), italics: true };
case 'delete':
return { text: convertInline(node.children), decoration: 'lineThrough' };
case 'inlineCode':
return { text: node.value, background: CODE_BACKGROUND };
case 'break':
return '\n';
case 'link':
return { text: convertInline(node.children), link: node.url, color: LINK_COLOR, decoration: 'underline' };
// Obrázky z internetu se do PDF nestahují, zůstane popisek
case 'image':
return node.alt ?? '';
case 'html':
return node.value;
default:
return 'children' in node ? convertInline(node.children as PhrasingContent[]) : '';
}
};
/** 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);