Created
December 2, 2021 14:57
-
-
Save nitwhiz/f540529252d9e7c55e1cb988f1144871 to your computer and use it in GitHub Desktop.
convert advent of code pages into markdown for tampermonkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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