Skip to content

Instantly share code, notes, and snippets.

@nitwhiz
Created December 2, 2021 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitwhiz/f540529252d9e7c55e1cb988f1144871 to your computer and use it in GitHub Desktop.
Save nitwhiz/f540529252d9e7c55e1cb988f1144871 to your computer and use it in GitHub Desktop.
convert advent of code pages into markdown for tampermonkey
// ==UserScript==
// @name AoC2MD
// @namespace https://madeby.nitwhiz.xyz/
// @version 1.0
// @description convert advent of code pages into markdown
// @author nitwhiz
// @match https://adventofcode.com/*
// @icon https://www.google.com/s2/favicons?domain=adventofcode.com
// @grant none
// ==/UserScript==
(function () {
'use strict';
const markdown = [];
const articles = Array.from(document.querySelectorAll('article'));
for (const art of articles) {
const children = Array.from(art.children);
for (const child of children) {
switch (child.tagName.toLowerCase()) {
case 'pre':
markdown.push(`\`\`\`text\n${child.textContent.trim()}\n\`\`\``);
break;
case 'h2':
markdown.push(`## ${child.textContent.replaceAll('---', '').trim()}`);
break;
default:
markdown.push(
child.innerHTML
.replaceAll('<code>', '`')
.replaceAll('</code>', '`')
.replaceAll('<em>', '**')
.replaceAll('</em>', '**')
.replace(/<(.*?)>/g, ''),
);
}
}
console.log(markdown.join('\n\n'));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment