Add script to convert tech talks

This commit is contained in:
Aditya Thakral 2021-08-25 10:10:38 -04:00
parent 11f588e1be
commit 1d2e912269
2 changed files with 108 additions and 0 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
/scripts/mdx-scripts/node_modules
/scripts/mdx-scripts/markdown-events
/scripts/mdx-scripts/markdown-news
/scripts/mdx-scripts/markdown-talks

View File

@ -0,0 +1,107 @@
const fs = require("fs");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM("");
global.window = window;
var showdown = require("showdown");
const converter = new showdown.Converter();
const libxmljs = require("libxmljs");
const MIRROR_URL = "http://mirror.csclub.uwaterloo.ca/csclub/";
try {
fs.mkdirSync("./markdown-talks/");
} catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
const file = fs
.readFileSync("../../media/index.xml", "utf8")
.replace(/<!DOCTYPE.*>/, "");
const xml = libxmljs.parseHtml(file);
const talks = xml.find("//mediaitem");
talks.forEach((talk, idx) => {
const { filename, markdown } = xml2md(talk, idx);
fs.promises.writeFile(`./markdown-talks/${filename}.md`, markdown);
});
/**
*
* @param {libxmljs.Element} talk
*/
function xml2md(talk, index) {
const title = talk.attr("title").value();
const abstract = talk
.get("abstract")
?.childNodes()
.reduce((str, node) => str + node.toString(), "")
.trim();
const presentors = talk
.get("presentor")
.childNodes()
.toString()
.split(",")
.map((s) => s.trim());
const thumbSmall = MIRROR_URL + talk.get("thumbnail").attr("file").value();
const thumbLarge = thumbSmall.replace("-thumb-small", "-thumb-large");
const links = talk
.find("mediafile")
.map((mf) => ({
/** @type{string} */
file: MIRROR_URL + mf.attr("file").value(),
/** @type{string} */
type: mf.attr("type").value(),
/** @type{string | undefined} */
size: mf.attr("size")?.value() ?? undefined,
}))
.map(
({ file, type, size }) =>
" " +
`
- file: '${file.replaceAll("'", "''")}'
type: '${type.replaceAll("'", "''")}'
${size == null ? "" : `size: '${size.replaceAll("'", "''")}'`}
`.trim(),
);
const markdown =
`
---
index: ${index}
title: '${title.replaceAll("'", "''")}'
presentors:
- '${presentors.join("\n - ").replaceAll("'", "''")}'
thumbnails:
small: '${thumbSmall}'
large: '${thumbLarge}'
links:
${links.join("\n")}
---
${converter.makeMarkdown(abstract ?? "")}
`.trim() + "\n";
return {
filename: title
.toLowerCase()
.replaceAll(" ", "-")
.replaceAll(":", "")
.replaceAll("=", "")
.replaceAll(",", "-")
.replaceAll("'", "-")
.replaceAll("+", "-plus-")
.replaceAll(/-+/g, "-"),
markdown,
};
}