-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude-html.mjs
More file actions
executable file
·51 lines (43 loc) · 1.41 KB
/
include-html.mjs
File metadata and controls
executable file
·51 lines (43 loc) · 1.41 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
// include-html.mjs
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const includeHtmlDirective = {
name: 'include-html',
doc: 'Include HTML from a file',
arg: {
type: String,
doc: 'The name of the HTML file to include (without .html extension)',
},
run(data) {
const filename = data.arg;
if (!filename) {
return [{ type: 'paragraph', children: [{ type: 'text', value: 'Error: Filename required' }] }];
}
// Skip HTML component rendering during PDF/Typst builds to avoid Typst compilation errors
if (process.env.MYST_PDF_BUILD === '1') {
return [];
}
try {
let htmlContent = readFileSync(join(__dirname, '_components', `${filename}.html`), 'utf8');
// Add wrapper div for component styling and auto-sizing
htmlContent = `<div class="component-wrapper" data-component="${filename}">
${htmlContent}
</div>`;
return [{ type: 'html', value: htmlContent }];
} catch (error) {
return [{
type: 'paragraph',
children: [{ type: 'text', value: `Error loading HTML file: ${error.message}` }]
}];
}
},
};
const plugin = {
name: 'HTML File Includer',
directives: [includeHtmlDirective]
};
export default plugin;