generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyaml-inliner.mjs
55 lines (46 loc) · 1.56 KB
/
yaml-inliner.mjs
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
// imports YAML files into the provided CSS
// usage: @import "./style-settings.yaml";
import fs from 'fs/promises';
import path from 'path';
import postcss from 'postcss';
export async function yamlInliner(inputCssPath, outputCssPath) {
const yamlInlinerPlugin = () => {
return {
postcssPlugin: 'yaml-inliner',
async Once(root) {
const promises = [];
root.walkAtRules('import', (rule) => {
if (rule.params.endsWith('.yaml"') || rule.params.endsWith('.yaml\'')) {
const yamlPath = rule.params.replace(/['"]/g, '');
const fullPath = path.resolve(path.dirname(inputCssPath), yamlPath);
promises.push(
fs.readFile(fullPath, 'utf8')
.then(yamlContent => {
rule.replaceWith({
text: `@settings\n\n${yamlContent}\n`
});
})
.catch(err => {
console.error(`[yaml-inliner] error reading YAML file: ${fullPath}`, err);
})
);
}
});
await Promise.all(promises);
}
};
};
yamlInlinerPlugin.postcss = true;
try {
const css = await fs.readFile(inputCssPath, 'utf8');
const result = await postcss([yamlInlinerPlugin()]).process(css, {
from: inputCssPath,
to: outputCssPath,
});
await fs.writeFile(outputCssPath, result.css);
console.log('[yaml-inliner] CSS processing complete');
} catch (error) {
console.error('[yaml-inliner] PostCSS processing failed:', error);
throw error;
}
}