Skip to content

Instantly share code, notes, and snippets.

@bkyle
Last active June 8, 2021 13:54
Show Gist options
  • Save bkyle/c22cb152245af341bf68f308458d181e to your computer and use it in GitHub Desktop.
Save bkyle/c22cb152245af341bf68f308458d181e to your computer and use it in GitHub Desktop.
mdNotes
(function() {
function highlight(span) {
const dataAnnotationContent = decodeURIComponent(span.getAttribute("data-annotation"))
const dataAnnotation = JSON.parse(dataAnnotationContent)
const citationItem = dataAnnotation.citationItem
const uri = dataAnnotation.uri
const id = uri.split("/").pop()
const text = dataAnnotation.text
const position = dataAnnotation.position
const pageIndex = position.pageIndex + 1
return {
"type": "highlight",
"uri": `zotero://open-pdf/library/items/${id}?page=${pageIndex}`,
"text": text,
}
}
function citation(span) {
const dataCitationContent = decodeURIComponent(span.getAttribute("data-citation"))
const dataCitation = JSON.parse(dataCitationContent)
const citationItems = dataCitation.citationItems
const citationItem = citationItems.length > 0 ? citationItems[0] : null
const uris = citationItem.uris
const uri = uris.length > 0 ? uris[0] : null
const id = uri.split("/").pop()
return {
"type":"citation",
"uri": `zotero://open-pdf/library/items/${id}`
}
}
let output = ""
function render(node, context = []) {
Zotero.log(context)
if (node instanceof NodeList) {
const nodes = node
for (let i=0; i<nodes.length; i++) {
render(nodes[i], context)
}
} else if (node instanceof Element) {
const t = node.tagName
const indent = context.indexOf("BLOCKQUOTE") > -1 ? "> " : ""
output += indent
if (t === "H1") {
context.push("H1")
output += "# "
render(node.childNodes, context)
output += "\n"
output += "\n"
context.pop()
} else if (t === "H2") {
context.push("H2")
output += "## "
render(node.childNodes, context)
output += "\n"
output += "\n"
context.pop()
} else if (t === "H3") {
context.push("H2")
output += "### "
render(node.childNodes, context)
output += "\n"
output += "\n"
context.pop()
} else if (t === "P") {
context.push("P")
render(node.childNodes, context)
output += "\n"
output += indent + "\n"
context.pop()
} else if (t === "A") {
context.push("A")
output += "["
render(node.childNodes, context)
output += "]()"
context.pop()
} else if (t === "B" || t === "STRONG") {
context.push("STRONG")
output += "**"
render(node.childNodes, context)
output += "**"
context.pop()
} else if (t === "I" || t === "EM") {
context.push("EM")
output += "_"
render(node.childNodes, context)
output += "_"
context.pop()
} else if (t === "OL") {
context.push("OL")
render(node.childNodes, context)
output += "\n"
context.pop()
} else if (t === "UL") {
context.push("UL")
render(node.childNodes, context)
output += "\n"
context.pop()
} else if (t === "LI") {
const bullet = context[context.length-1] === "OL" ? "1. " : "* "
context.push("LI")
output += bullet
render(node.childNodes, context)
output += "\n"
context.pop()
} else if (t === "PRE") {
context.push("PRE")
output += "```\n"
render(node.childNodes, context)
output += "\n```\n\n"
context.pop()
} else if (t === "BLOCKQUOTE") {
context.push("BLOCKQUOTE")
render(node.childNodes, context)
output += "\n\n"
context.pop()
} else {
render(node.childNodes, context)
}
}
if (node instanceof Text) {
output += node.nodeValue.replace(/\n/g, "")
}
return output
}
function xrender(node, context = {}) {
// TODO: Handle Citations
// TODO: Handle Highlights
// TODO: Handle prefixing child elements on a per-line basis: either
// pass the prefix to child calls, or have the child calls return
// a list and have the parent call do the prefixing.
if (node instanceof NodeList) {
} else if (node instanceof Element) {
Zotero.log(node.tagName)
if (node.tagName === "P") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
buffer = buffer.reduce((acc, elem) => {
if (acc.length === 0) {
acc.push(elem)
} else if (typeof(acc[acc.length-1]) === "string" && typeof(elem) === "string") {
Zotero.log("combine")
acc[acc.length-1] = acc[acc.length-1] + elem
} else {
acc.push(elem)
}
return acc
}, [])
return ["p", buffer]
} else if (node.tagName === "A") {
let text = ""
for (let i=0; i<node.childNodes.length; i++) {
text += render(node.childNodes[i])
}
let href = node.getAttribute("href")
return `[${text}](${href})`
} else if (node.tagName === "SPAN") {
} else if (node.tagName === "B" || node.tagName === "STRONG") {
let buffer = []
buffer.push("**")
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
buffer.push("**")
return buffer.join("")
} else if (node.tagName === "I" || node.tagName === "EM") {
let buffer = []
buffer.push("_")
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
buffer.push("_")
return buffer.join("")
} else if (node.tagName === "UL") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
if (node.childNodes[i] instanceof Element) {
buffer.push(render(node.childNodes[i]))
}
}
return ["*", buffer]
} else if (node.tagName === "OL") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
if (node.childNodes[i] instanceof Element) {
buffer.push(render(node.childNodes[i]))
}
}
return ["1.", buffer]
// } else if (node.tagName === "LI") {
} else if (node.tagName === "H1") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
return ["#", buffer]
} else if (node.tagName === "H2") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
return ["##", buffer]
} else if (node.tagName === "H3") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
return ["###", buffer]
} else if (node.tagName === "PRE") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
return ["```", buffer]
} else if (node.tagName === "BLOCKQUOTE") {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
let line = render(node.childNodes[i])
if (line) {
buffer.push(line)
}
}
return [">", buffer]
} else if (node.tagName === "BR") {
return ""
} else {
let buffer = []
for (let i=0; i<node.childNodes.length; i++) {
buffer.push(render(node.childNodes[i]))
}
return buffer
}
} else if (node instanceof Text) {
return node.nodeValue.replace(/\n/g, "")
} else {
return ""
}
}
const domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser)
const noteContent = Zotero.getActiveZoteroPane().getSelectedItems()[0].note
const fullDomNoteBody = domParser.parseFromString(noteContent, "text/html").body;
return render(fullDomNoteBody)
// return fullDomNoteBody
// const fullDomNote = fullDomNoteBody.childNodes;
// const spans = fullDomNoteBody.getElementsByTagName("span")
// const highlights = []
// for (let i=0; i<spans.length; i++) {
// let span = spans[i]
// if (span.getAttribute("data-citation")) {
// highlights.push(citation(span))
// } else if (span.getAttribute("data-annotation")) {
// highlights.push(highlight(span))
// }
// }
// return highlights
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment