feat: export částečných receptů

This commit is contained in:
Ondřej Anděl committed 2026-11-06 13:39:45 +01:00
1 parent 5e76c404fb
commit 8377ce2205
16 files changed
+446 -147

No files matched your search

+23 -13
View File
@@ -1,6 +1,6 @@
import { fromMarkdown } from "mdast-util-from-markdown";
import type { BlockContent, DefinitionContent, ListItem, PhrasingContent, RootContent } from "mdast";
import type { Content } from "pdfmake/interfaces";
import type { Content, ContentText } from "pdfmake/interfaces";
import { applyLineBreaks } from "./markdownBreaks";
/** Barva odkazů a citací — stejná jako text vedle, jen tlumenější. */
@@ -8,31 +8,41 @@ const MUTED_COLOR = '#6b7280';
const LINK_COLOR = '#15803d';
const CODE_BACKGROUND = '#f3f4f6';
const convertInline = (nodeList: PhrasingContent[]): Content[] => nodeList.map(convertPhrasing);
/** 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'>;
const convertPhrasing = (node: PhrasingContent): Content => {
/**
* 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 node.value;
return [{ ...style, text: node.value }];
case 'strong':
return { text: convertInline(node.children), bold: true };
return convertInline(node.children, { ...style, bold: true });
case 'emphasis':
return { text: convertInline(node.children), italics: true };
return convertInline(node.children, { ...style, italics: true });
case 'delete':
return { text: convertInline(node.children), decoration: 'lineThrough' };
return convertInline(node.children, { ...style, decoration: 'lineThrough' });
case 'inlineCode':
return { text: node.value, background: CODE_BACKGROUND };
return [{ ...style, text: node.value, background: CODE_BACKGROUND }];
case 'break':
return '\n';
return [{ ...style, text: '\n' }];
case 'link':
return { text: convertInline(node.children), link: node.url, color: LINK_COLOR, decoration: 'underline' };
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 node.alt ?? '';
return [{ ...style, text: node.alt ?? '' }];
case 'html':
return node.value;
return [{ ...style, text: node.value }];
default:
return 'children' in node ? convertInline(node.children as PhrasingContent[]) : '';
return 'children' in node ? convertInline(node.children as PhrasingContent[], style) : [];
}
};