-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_image.js
107 lines (95 loc) · 3.12 KB
/
create_image.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const fs = require('fs');
const path = require('path');
const { convertFile } = require('convert-svg-to-png');
// Set the path to the "content.en" folder
const folderPath = 'content.en';
// Set the path to the SVG template file
const svgTemplatePath = 'image.svg';
// Read the "content.en" folder structure
function readFolderStructure(folderPath) {
const files = fs.readdirSync(folderPath);
files.forEach(file => {
const filePath = path.join(folderPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
readFolderStructure(filePath);
} else {
if (path.extname(filePath) === '.md') {
console.log(`${filePath} : Processing file`);
processMarkdownFile(filePath);
}
}
});
}
// Process each Markdown file
function processMarkdownFile(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const title = getFrontMatter(fileContent);
if (title) {
const fileName = path.basename(filePath, '.md');
const svgFilePath = path.join(path.dirname(filePath), `${fileName}.svg`);
replaceTitleInSVG(svgFilePath, title);
console.log(`${svgFilePath} : Replaced title in SVG file for "${title}"`);
} else {
console.error(`Error : ${filePath} : No title Found:`);
}
} catch (error) {
console.error(`Error : ${filePath} : Error processing file:`, error);
}
}
// Extract YAML front matter from Markdown content
function getFrontMatter(content) {
const regex = /^---\n([\s\S]+?)\n---\n/;
const matches = content.match(regex);
if (matches && matches[1]) {
return parseTitle(matches[1]);
}
return null;
}
// Parse YAML front matter
function parseTitle(yamlString) {
const lines = yamlString.split('\n');
// take first and split using colon and take second part
let title = lines[0].split(':')[1].trim();
// remove quotes
if (title.startsWith('"') && title.endsWith('"')) {
title = title.substring(1, title.length - 1);
}
return title;
}
// Replace the title in the SVG file
function replaceTitleInSVG(svgFilePath, title) {
const svgTemplate = fs.readFileSync(svgTemplatePath, 'utf-8');
const wrappedTitle = wrapSvgText(title, 30);
const replacedSVG = svgTemplate.replace('REPLACE_TITLE', createTspanElements(wrappedTitle));
fs.writeFileSync(svgFilePath, replacedSVG);
(async () => {
const outputFilePath = convertFile(svgFilePath);
console.log(`${outputFilePath} : Converted SVG to PNG`);
})();
}
// Wrap text if it exceeds the specified width
function wrapSvgText(text, width) {
const words = text.split(' ');
const lines = [];
let currentLine = words[0];
for (let i = 1; i < words.length; i++) {
if ((currentLine.length + words[i].length + 1) <= width) {
currentLine += ' ' + words[i];
} else {
lines.push(currentLine);
currentLine = words[i];
}
}
lines.push(currentLine);
return lines;
}
function createTspanElements(lines) {
return lines.map((line, index) => `<tspan
x="67.792046"
y="${index * 15 + 30.436655}"
id="tspan867">${line}</tspan>`).join('');
}
// Start reading the "content.en" folder structure
readFolderStructure(folderPath);