Skip to content

Instantly share code, notes, and snippets.

@shotasenga
Last active September 6, 2019 22:57
Show Gist options
  • Save shotasenga/8c197a7fe46373b641f331ee48628e39 to your computer and use it in GitHub Desktop.
Save shotasenga/8c197a7fe46373b641f331ee48628e39 to your computer and use it in GitHub Desktop.
highlight code block on Dynalist
// ==UserScript==
// forked from https://talk.dynalist.io/t/multi-line-code-blocks/41/14?u=shota_senga
//
// @name DynaHighlight
// @namespace http://tampermonkey.net/
// @version 0.2
// @author Piotr S.
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @require https://cdnjs.cloudflare.com/ajax/libs/keyboardjs/2.3.3/keyboard.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/highlight.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/languages/typescript.min.js
// @match https://dynalist.io/d/*
// @description Highlighting code for Dynalist
// ==/UserScript==
;(function() {
// https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/styles/atelier-dune-light.min.css
// Atom One Dark Reasonable
// Atom One Light
// Github Gist
$("head").append(
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/styles/atom-one-dark-reasonable.min.css">'
)
const highlightCode = () => {
$(".node-inline-code").each(function(i, block) {
const firstLine = block.text().split("\n")[0]
if ($.inArray(firstLine, hljs.listLanguages()) >= 0) {
block.addClass(firstLine)
block.text(
block
.text()
.split("\n")
.slice(1)
.join("\n")
)
hljs.highlightBlock(block)
}
})
}
let interval = setInterval(function() {
let doc = DYNALIST.app.get_current_app_document()
if (doc !== null && doc.document.node_collection.available) {
highlightCode()
keyboardJS.bind("ctrl + alt + h", function(e) {
e.preventDefault()
highlightCode()
})
clearInterval(interval)
}
}, 200)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment