Created
May 6, 2018 22:06
-
-
Save helton/33d5c10cfc5e9be29a8435bcd21a7db1 to your computer and use it in GitHub Desktop.
Simple Markdown for Quizlet description (info page)
This file contains hidden or 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
const markdown = raw => { | |
const createBulletList = content => { | |
const regex = /- /gi | |
return content.match(regex).reduce((body, bullet) => body.replace(bullet, `<pre style="display: inline-block">• </pre>`), content) | |
} | |
const createLinks = content => { | |
const regex = /\[(.*?)\]\((https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)(\?hl=[a-z]{2})?)\)/gi | |
let result = content | |
let match = regex.exec(content) | |
while (match !== null) { | |
const [all, description, link] = match | |
result = result.replace(all, `<a href=${link} target="_blank">${description}</a>`) | |
match = regex.exec(content); | |
} | |
return result | |
} | |
const createTitles = content => { | |
const regex = /#.*/gi | |
return content.match(regex).reduce((body, title) => body.replace(title, `<strong>${title.slice(1)}</strong>`), content) | |
} | |
return createTitles(createLinks(createBulletList(raw))) | |
} | |
const descriptionBody = document.querySelector('.SetPageInfoModal-descriptionBody') | |
if (descriptionBody) { | |
descriptionBody.innerHTML = markdown(descriptionBody.innerHTML) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment