-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypst-tag.js
More file actions
59 lines (53 loc) · 1.73 KB
/
typst-tag.js
File metadata and controls
59 lines (53 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* typst-tag.js - Hexo tag for rendering Typst code
*/
const compileTypst = require('./lib/typst-compiler');
/**
* Default Typst wrapper that adds page settings
* @param {string} code - Raw Typst code
* @returns {string} - Wrapped Typst code
*/
function wrapTypstCode(code) {
return `#set page(width: auto, height: auto, margin: 0in, fill: rgb("#f9fafb"))
#set text(font: ("Fira Sans", "LXGW Wenkai"), size: 16pt)
${code}`;
}
/**
* Default SVG wrapper that adds necessary HTML and styling
* @param {string} svg - SVG code
* @returns {string} - Wrapped SVG with styling
*/
function wrapSvgOutput(svg) {
// 替换SVG内部的白色背景为#f9fafb
svg = svg.replace(/fill="white"/gi, 'fill="#f9fafb"')
.replace(/background:white/gi, 'background:#f9fafb')
.replace(/fill:#fff/gi, 'fill:#f9fafb')
.replace(/fill:#ffffff/gi, 'fill:#f9fafb');
return `<div class="typst-container" style="background:#f9fafb;">
<div class="typst-svg">
${svg}
</div>
</div>`;
}
/**
* Typst tag handler
*/
module.exports = function (ctx) {
return function typstTag(args, content) {
// Parse tag options (future enhancement)
const options = {};
try {
// Prepare Typst code
const typstCode = wrapTypstCode(content);
// Compile to SVG
const svg = compileTypst(typstCode);
// Return wrapped SVG
let v = wrapSvgOutput(svg);
// console.warn("[typst-tag] SVG output:", v);
return '' + wrapSvgOutput(svg) + '';
} catch (error) {
console.error("[typst-tag] Error:", error);
return `<div class="typst-error">Error rendering Typst: ${error.message}</div>`;
}
};
};