Skip to content

Instantly share code, notes, and snippets.

@pketh
Forked from danreeves/kinopio-markdown-userscript.js
Last active January 3, 2021 04:08
Show Gist options
  • Save pketh/11c2441a5e61ef069b9e00fc23211f0f to your computer and use it in GitHub Desktop.
Save pketh/11c2441a5e61ef069b9e00fc23211f0f to your computer and use it in GitHub Desktop.
kinopio.club markdown userscript
// ==UserScript==
// @name Kinopio Markdown
// @namespace https://kinopio.club/kinopio-markdown-userscript-Q9qnmvXEPaagLx5Ufytlx
// @version 0.1
// @description convert text in cards to markdown
// @author Pirijan (forked from dnrvs)
// @match https://kinopio.club/kinopio-markdown-userscript-Q9qnmvXEPaagLx5Ufytlx
// @grant none
// ==/UserScript==
(function() {
'use strict';
function kinopioExtension() {
let dataAttr = 'data-kinopio-markdown-extension'
function processCard(container, node, text) {
// Clean out old nodes
container.querySelectorAll(`[${dataAttr}]`).forEach(n => n.remove())
// Clone the real one
let newNode = node.cloneNode(true /* deep */ )
// Make sure the real one is hidden
node.style.setProperty('display', 'none')
// Set up our node
newNode.setAttribute(dataAttr, true)
// Ensure it's visble
newNode.style.setProperty('display', 'block')
// Set it's content
newNode.innerHTML = parse(text)
// Insert it after the real one
node.after(newNode)
}
function handler(events) {
for (let e of events) {
if (e.type === "characterData") {
let container = e.target?.parentElement?.parentElement
let node = e.target?.parentElement
if (container?.classList.contains("card-content-wrap")) {
if (!node.hasAttribute(dataAttr)) {
processCard(container, node, e.target)
}
}
}
}
}
new MutationObserver(handler).observe(document.body, {
attributes: false,
childList: true,
subtree: true,
characterData: true
})
// get all the name segment nodes in each card container
document.querySelectorAll('.card-content-wrap').forEach(container => {
let nodes = container.querySelectorAll('.name span')
// filter out tag nodes
nodes = Array.from(nodes).filter(node => node.classList.length === 0)
nodes.forEach(node => {
const text = node.textContent
processCard(container, node, text)
})
})
document.addEventListener("click", event => {
if (event.target.nodeName === "A") {
if (event.target.parentElement.hasAttribute(dataAttr)) {
event.stopPropagation()
event.stopImmediatePropagation()
event.preventDefault()
window.open(event.target.getAttribute("href"), "_blank")
}
}
})
}
/*
https://github.com/mrvautin/downa
Modified by @dnrvs
MIT License
Copyright (c) 2018 Mark Moffat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const linksRe = /\[([^[]+)\]\(([^)]+)\)/gmi;
const boldRe = /(\*)(.*?)\1/gmi;
const emphasisRe = /(\_)(.*?)\1/gmi;
const delRe = /~(.*?)~/gmi;
const quoteRe = /:"(.*?)":/gmi;
const inlineCodeRe = /`([^`]+)`/gmi;
const rules = {
links: {
regex: linksRe,
replacer: function(match, $1, $2) {
return `<a href="${$2}">${$1}</a>`;
}
},
bold: {
regex: boldRe,
replacer: function(match, $1, $2) {
return `<strong>${$2}</strong>`;
}
},
emphasis: {
regex: emphasisRe,
replacer: function(match, $1, $2) {
return `<em>${$2}</em>`;
}
},
del: {
regex: delRe,
replacer: function(match, $1, $2) {
return `<del>${$1}</del>`;
}
},
quote: {
regex: quoteRe,
replacer: function(match, $1, $2) {
return `<q>${$1}</q>`;
}
},
inlineCode: {
regex: inlineCodeRe,
replacer: function(match, $1, $2) {
return `<code>${$1}</code>`;
}
},
};
/**
* @param {String} markdown A string of Markdown content
* @returns {String} A HTML formatted string
*/
function parse(markdown) {
Object.keys(rules).forEach((key) => {
markdown = markdown.replace(rules[key].regex, rules[key].replacer);
});
return markdown;
};
// Lets goooo~
kinopioExtension()
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment