Compare commits

...

11 Commits

Author SHA1 Message Date
Wen Junhua c24275f73e
Merge a19dc47b6a into 3bdb8f768c 2024-02-04 18:20:19 -05:00
Wen Junhua a19dc47b6a
Merge branch 'dev' into feat/add-icon-script 2024-01-23 23:34:35 +08:00
Wen Junhua 2cf1371c3e fix: remove debug logs 2023-12-31 13:26:09 +08:00
Wen Junhua a1a2e10481
Merge branch 'dev' into feat/add-icon-script 2023-12-31 13:24:13 +08:00
Wen Junhua 97a3f0064c feat: port script to node 2023-12-31 13:24:02 +08:00
Wen Junhua a7fa3be37d
Merge branch 'dev' into feat/add-icon-script 2023-12-26 17:19:40 +09:00
Wen Junhua 679898570c docs: update script docs 2023-12-25 23:27:14 +09:00
Wen Junhua db07288d01 chore: clean up code 2023-12-25 23:24:47 +09:00
Wen Junhua 3c81e00bed chore: clean up code 2023-12-25 23:17:40 +09:00
Wen Junhua eb527de54b feat: add docs update 2023-12-25 23:13:32 +09:00
Wen Junhua 3a998b4598 feat: add script to auto update icons 2023-12-25 22:17:25 +09:00
4 changed files with 840 additions and 15 deletions

743
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,8 @@
"dev": "NODE_ENV=development ./node_modules/tailwindcss/lib/cli.js -i ./assets/css/main.css -o ./assets/css/compiled/main.css --jit -w",
"build": "NODE_ENV=production ./node_modules/tailwindcss/lib/cli.js -i ./assets/css/main.css -o ./assets/css/compiled/main.css --jit",
"example": "hugo server --bind 0.0.0.0 -p 8008 --source exampleSite --themesDir ../.. --buildDrafts -b http://localhost/congo/ ",
"lighthouse": "lhci autorun"
"lighthouse": "lhci autorun",
"add-icon": "node scripts/index.js add-icon"
},
"repository": {
"type": "git",
@ -32,6 +33,7 @@
"@tailwindcss/typography": "^0.5.10",
"chart.js": "^4.4.1",
"fuse.js": "^7.0.0",
"jsdom": "^23.0.1",
"katex": "^0.16.9",
"mermaid": "^10.7.0",
"prettier": "^3.2.4",

11
scripts/index.js 100644
View File

@ -0,0 +1,11 @@
#!/usr/bin/env node
const { program } = require("commander");
const { add_icon_to_congo } = require("./update_icon");
// Adds an icon to the project.
program
.command("add-icon <icon_name>")
.description("Add icon to the project")
.action(add_icon_to_congo);
program.parse(process.argv);

View File

@ -0,0 +1,97 @@
const jsdom = require("jsdom");
const fs = require("fs");
const DOC_DIR = "./exampleSite/content/samples/icons";
const FONTAWESOME_VERSION = "v6.5.1";
const DEFAULT_TABLE_DELIMITER = "| -------------------- | --------------------------------- |";
/**
* Saves an icon to the congo project and update documentation.
* @param {string} icon_name Icon name from Font Awesome to download
* @returns null
*/
const add_icon_to_congo = async (icon_name) => {
try {
const icon_url = create_icon_url(icon_name, FONTAWESOME_VERSION);
const file = await get_file(icon_url);
const final_svg = modify_svg_string(file);
const icon_download_path = create_icon_download_path(icon_name);
save_file(icon_download_path, final_svg);
add_documentation(icon_name);
} catch (e) {
console.log(e);
return;
}
};
const modify_svg_string = (svg_string) => {
try {
dom = new jsdom.JSDOM(svg_string);
svg = dom.window.document.documentElement.querySelector("svg");
svg.querySelector("path").setAttribute("fill", "currentColor");
return svg.outerHTML;
} catch (e) {
throw new Error("Invalid SVG file");
}
};
const create_icon_url = (icon_name, fontawesome_version) => {
return `https://site-assets.fontawesome.com/releases/${fontawesome_version}/svgs/brands/${icon_name}.svg`;
};
const create_icon_download_path = (icon_name) => {
return `./assets/icons/${icon_name}.svg`;
};
const get_file = async (url) => {
console.log("Getting file at " + url + "...");
const response = await fetch(url);
if (response.status >= 400) {
throw new Error("Could not download icon / icon not found");
}
console.log("File retrieved!");
return response.text();
};
const save_file = (file_path, file) => {
console.log("Saving file at " + file_path + "...");
fs.writeFile(file_path, file, function (err) {
if (err) throw err;
console.log("File saved!");
});
};
const add_documentation = async (icon_name) => {
files = get_md_docs();
for (const file of files) {
const file_path = `${DOC_DIR}/${file}`;
const file_contents = fs.readFileSync(file_path, "utf8");
const file_result = process_file(file_contents, icon_name);
// Save file_result to file_path
fs.writeFile(file_path, file_result, function (err) {
if (err) throw err;
});
console.log(`Updated ${file_path}`);
}
};
/**
* Process the file contents to include the icon name.
* @param {string} file_contents contents of the documentation files.
* @returns {string} processed file contents.
*/
const process_file = (file_contents, icon_name) => {
const [headers, table] = file_contents.split(DEFAULT_TABLE_DELIMITER);
const table_rows = table.split("\n").map((x) => x.trim()).filter((row) => row !== "");
table_rows.push(table_rows[0].replace("amazon", icon_name));
table_rows.sort();
const new_table = table_rows.join("\n");
return `${headers.trimEnd()}\n${DEFAULT_TABLE_DELIMITER}\n${new_table}\n`;
};
const get_md_docs = () => {
return fs.readdirSync(DOC_DIR).filter((file) => file.endsWith(".md"));
};
module.exports = { add_icon_to_congo };