Skip to content

Commit f4e8a0d

Browse files
AmirMohammad CheraghaliAmirMohammad Cheraghali
authored andcommitted
fix: resolve Chemical Sketcher PDF rendering by using Base64 serialization for data integrity
1 parent 9e0a0f2 commit f4e8a0d

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/components/dashboard/ChemicalSketcher.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export const ChemicalSketcher = Node.create({
6060
return {
6161
markdown: {
6262
serialize: (state: any, node: any) => {
63-
state.write(`[[sketcher:${node.attrs.molfile}###SKETCH_SEP###${node.attrs.svg}]]`);
63+
// Base64 encode to prevent newlines from breaking Markdown paragraph parsing
64+
const payload = JSON.stringify({
65+
mol: node.attrs.molfile,
66+
svg: node.attrs.svg
67+
});
68+
const base64 = btoa(unescape(encodeURIComponent(payload)));
69+
state.write(`[[sketcher:${base64}]]`);
6470
state.closeBlock(node);
6571
},
6672
parse: {

src/components/dashboard/LabReportTemplate.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,29 @@ export const LabReportTemplate = React.forwardRef<HTMLDivElement, LabReportTempl
182182
);
183183
}
184184

185-
// Handle Chemical Sketcher
186-
const sketchMatch = part.match(/\[\[sketcher:([\s\S]*?)###SKETCH_SEP###([\s\S]*?)\]\]/);
185+
// Handle Chemical Sketcher (Base64 version)
186+
const sketchMatch = part.match(/\[\[sketcher:([A-Za-z0-9+/=]+)\]\]/);
187187
if (sketchMatch) {
188-
const svgContent = sketchMatch[2];
189-
return (
190-
<div
191-
key={i}
192-
className="my-4 p-4 border border-gray-100 rounded-xl bg-gray-50/50"
193-
style={{ width: '100%', display: 'flex', justifyContent: 'center' }}
194-
dangerouslySetInnerHTML={{
195-
__html: svgContent.replace('<svg', '<svg style="width:100%; height:auto; max-height:280px" preserveAspectRatio="xMidYMid meet"')
196-
}}
197-
/>
198-
);
188+
try {
189+
const base64 = sketchMatch[1];
190+
const decoded = decodeURIComponent(escape(atob(base64)));
191+
const payload = JSON.parse(decoded);
192+
const svgContent = payload.svg;
193+
194+
return (
195+
<div
196+
key={i}
197+
className="my-4 p-4 border border-gray-100 rounded-xl bg-gray-50/50"
198+
style={{ width: '100%', display: 'flex', justifyContent: 'center' }}
199+
dangerouslySetInnerHTML={{
200+
__html: svgContent.replace('<svg', '<svg style="width:100%; height:auto; max-height:280px" preserveAspectRatio="xMidYMid meet"')
201+
}}
202+
/>
203+
);
204+
} catch (err) {
205+
console.error("PDF Sketcher Decode Error:", err);
206+
return <span className="text-red-400 text-[10px]">Error rendering structure</span>;
207+
}
199208
}
200209

201210
return part;

0 commit comments

Comments
 (0)