2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-env browser, jquery */
|
2019-05-30 18:27:56 -04:00
|
|
|
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */
|
2020-08-13 17:41:44 -04:00
|
|
|
/* global moment, serverurl */
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
import Prism from 'prismjs'
|
|
|
|
import PDFObject from 'pdfobject'
|
|
|
|
import S from 'string'
|
|
|
|
import { saveAs } from 'file-saver'
|
|
|
|
import escapeHTML from 'escape-html'
|
|
|
|
|
|
|
|
import getUIElements from './lib/editor/ui-elements'
|
|
|
|
|
|
|
|
import markdownit from 'markdown-it'
|
|
|
|
import markdownitContainer from 'markdown-it-container'
|
|
|
|
|
|
|
|
/* Defined regex markdown it plugins */
|
|
|
|
import Plugin from 'markdown-it-regexp'
|
|
|
|
|
2021-06-06 12:10:38 -04:00
|
|
|
import 'gist-embed'
|
2021-06-06 11:44:17 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
require('prismjs/themes/prism.css')
|
|
|
|
require('prismjs/components/prism-wiki')
|
|
|
|
require('prismjs/components/prism-haskell')
|
|
|
|
require('prismjs/components/prism-go')
|
|
|
|
require('prismjs/components/prism-typescript')
|
|
|
|
require('prismjs/components/prism-jsx')
|
2017-03-26 11:09:13 -04:00
|
|
|
require('prismjs/components/prism-makefile')
|
2017-05-05 06:03:23 -04:00
|
|
|
require('prismjs/components/prism-gherkin')
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
require('./lib/common/login')
|
2020-08-13 17:41:44 -04:00
|
|
|
require('./locale')
|
2017-03-08 13:41:05 -05:00
|
|
|
require('../vendor/md-toc')
|
2017-03-28 07:30:06 -04:00
|
|
|
const ui = getUIElements()
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// auto update last change
|
|
|
|
window.createtime = null
|
|
|
|
window.lastchangetime = null
|
2016-10-13 03:13:03 -04:00
|
|
|
window.lastchangeui = {
|
2020-08-13 17:55:28 -04:00
|
|
|
statusChanged: $('.ui-status-lastchange.changed'),
|
|
|
|
statusCreated: $('.ui-status-lastchange.created'),
|
2017-03-08 13:41:05 -05:00
|
|
|
time: $('.ui-lastchange'),
|
|
|
|
user: $('.ui-lastchangeuser'),
|
|
|
|
nouser: $('.ui-no-lastchangeuser')
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
const ownerui = $('.ui-owner')
|
2017-01-05 04:52:32 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function updateLastChange () {
|
|
|
|
if (!window.lastchangeui) return
|
|
|
|
if (window.createtime) {
|
|
|
|
if (window.createtime && !window.lastchangetime) {
|
2020-08-13 17:55:28 -04:00
|
|
|
window.lastchangeui.statusChanged.hide()
|
|
|
|
window.lastchangeui.statusCreated.show()
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2020-08-13 17:55:28 -04:00
|
|
|
window.lastchangeui.statusChanged.show()
|
|
|
|
window.lastchangeui.statusCreated.hide()
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
const time = window.lastchangetime || window.createtime
|
|
|
|
window.lastchangeui.time.html(moment(time).fromNow())
|
|
|
|
window.lastchangeui.time.attr('title', moment(time).format('llll'))
|
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
setInterval(updateLastChange, 60000)
|
|
|
|
|
|
|
|
window.lastchangeuser = null
|
|
|
|
window.lastchangeuserprofile = null
|
|
|
|
|
|
|
|
export function updateLastChangeUser () {
|
|
|
|
if (window.lastchangeui) {
|
|
|
|
if (window.lastchangeuser && window.lastchangeuserprofile) {
|
|
|
|
const icon = window.lastchangeui.user.children('i')
|
|
|
|
icon.attr('title', window.lastchangeuserprofile.name).tooltip('fixTitle')
|
|
|
|
if (window.lastchangeuserprofile.photo) { icon.attr('style', `background-image:url(${window.lastchangeuserprofile.photo})`) }
|
|
|
|
window.lastchangeui.user.show()
|
|
|
|
window.lastchangeui.nouser.hide()
|
|
|
|
} else {
|
|
|
|
window.lastchangeui.user.hide()
|
|
|
|
window.lastchangeui.nouser.show()
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
window.owner = null
|
|
|
|
window.ownerprofile = null
|
|
|
|
|
|
|
|
export function updateOwner () {
|
2017-03-22 03:42:11 -04:00
|
|
|
if (ownerui) {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (window.owner && window.ownerprofile && window.owner !== window.lastchangeuser) {
|
|
|
|
const icon = ownerui.children('i')
|
|
|
|
icon.attr('title', window.ownerprofile.name).tooltip('fixTitle')
|
|
|
|
const styleString = `background-image:url(${window.ownerprofile.photo})`
|
|
|
|
if (window.ownerprofile.photo && icon.attr('style') !== styleString) { icon.attr('style', styleString) }
|
|
|
|
ownerui.show()
|
|
|
|
} else {
|
|
|
|
ownerui.hide()
|
2016-10-10 08:32:20 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-10-10 08:32:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// get title
|
|
|
|
function getTitle (view) {
|
|
|
|
let title = ''
|
|
|
|
if (md && md.meta && md.meta.title && (typeof md.meta.title === 'string' || typeof md.meta.title === 'number')) {
|
|
|
|
title = md.meta.title
|
|
|
|
} else {
|
|
|
|
const h1s = view.find('h1')
|
|
|
|
if (h1s.length > 0) {
|
|
|
|
title = h1s.first().text()
|
2015-05-04 03:53:29 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
title = null
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
return title
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// render title
|
|
|
|
export function renderTitle (view) {
|
|
|
|
let title = getTitle(view)
|
|
|
|
if (title) {
|
2020-07-02 11:22:52 -04:00
|
|
|
title += ' - HedgeDoc'
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2020-07-02 11:22:52 -04:00
|
|
|
title = 'HedgeDoc - Collaborative markdown notes'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
return title
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// render filename
|
|
|
|
export function renderFilename (view) {
|
|
|
|
let filename = getTitle(view)
|
|
|
|
if (!filename) {
|
|
|
|
filename = 'Untitled'
|
|
|
|
}
|
|
|
|
return filename
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2016-10-19 10:10:33 -04:00
|
|
|
// render tags
|
2017-03-08 13:41:05 -05:00
|
|
|
export function renderTags (view) {
|
|
|
|
const tags = []
|
|
|
|
const rawtags = []
|
|
|
|
if (md && md.meta && md.meta.tags && (typeof md.meta.tags === 'string' || typeof md.meta.tags === 'number')) {
|
|
|
|
const metaTags = (`${md.meta.tags}`).split(',')
|
|
|
|
for (let i = 0; i < metaTags.length; i++) {
|
|
|
|
const text = metaTags[i].trim()
|
|
|
|
if (text) rawtags.push(text)
|
2016-10-19 10:10:33 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
|
|
|
view.find('h6').each((key, value) => {
|
|
|
|
if (/^tags/gmi.test($(value).text())) {
|
|
|
|
const codes = $(value).find('code')
|
|
|
|
for (let i = 0; i < codes.length; i++) {
|
|
|
|
const text = codes[i].innerHTML.trim()
|
|
|
|
if (text) rawtags.push(text)
|
2016-10-19 10:10:33 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for (let i = 0; i < rawtags.length; i++) {
|
|
|
|
let found = false
|
|
|
|
for (let j = 0; j < tags.length; j++) {
|
|
|
|
if (tags[j] === rawtags[i]) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
2016-10-19 10:10:33 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!found) { tags.push(rawtags[i]) }
|
|
|
|
}
|
|
|
|
return tags
|
2016-10-19 10:10:33 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function slugifyWithUTF8 (text) {
|
2019-10-21 02:26:42 -04:00
|
|
|
// remove HTML tags and trim spaces
|
2018-01-16 02:59:43 -05:00
|
|
|
let newText = S(text).trim().stripTags().s
|
2019-10-21 02:26:42 -04:00
|
|
|
// replace space between words with dashes
|
2018-01-16 02:59:43 -05:00
|
|
|
newText = newText.replace(/\s+/g, '-')
|
2019-10-21 02:26:42 -04:00
|
|
|
// slugify string to make it valid as an attribute
|
2017-03-08 13:41:05 -05:00
|
|
|
newText = newText.replace(/([!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~])/g, '')
|
|
|
|
return newText
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function isValidURL (str) {
|
2020-10-30 13:40:09 -04:00
|
|
|
try {
|
|
|
|
const url = new URL(str)
|
|
|
|
return ['http:', 'https:'].includes(url.protocol)
|
|
|
|
} catch (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
return false
|
|
|
|
}
|
2016-06-21 09:44:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// parse meta
|
|
|
|
export function parseMeta (md, edit, view, toc, tocAffix) {
|
|
|
|
let lang = null
|
|
|
|
let dir = null
|
|
|
|
let breaks = true
|
|
|
|
if (md && md.meta) {
|
|
|
|
const meta = md.meta
|
|
|
|
lang = meta.lang
|
|
|
|
dir = meta.dir
|
|
|
|
breaks = meta.breaks
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// text language
|
2017-03-08 13:41:05 -05:00
|
|
|
if (lang && typeof lang === 'string') {
|
|
|
|
view.attr('lang', lang)
|
|
|
|
toc.attr('lang', lang)
|
|
|
|
tocAffix.attr('lang', lang)
|
|
|
|
if (edit) { edit.attr('lang', lang) }
|
|
|
|
} else {
|
|
|
|
view.removeAttr('lang')
|
|
|
|
toc.removeAttr('lang')
|
|
|
|
tocAffix.removeAttr('lang')
|
|
|
|
if (edit) { edit.removeAttr('lang', lang) }
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// text direction
|
2017-03-08 13:41:05 -05:00
|
|
|
if (dir && typeof dir === 'string') {
|
|
|
|
view.attr('dir', dir)
|
|
|
|
toc.attr('dir', dir)
|
|
|
|
tocAffix.attr('dir', dir)
|
|
|
|
} else {
|
|
|
|
view.removeAttr('dir')
|
|
|
|
toc.removeAttr('dir')
|
|
|
|
tocAffix.removeAttr('dir')
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// breaks
|
2017-03-08 13:41:05 -05:00
|
|
|
if (typeof breaks === 'boolean' && !breaks) {
|
|
|
|
md.options.breaks = false
|
|
|
|
} else {
|
|
|
|
md.options.breaks = true
|
|
|
|
}
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
window.viewAjaxCallback = null
|
|
|
|
|
|
|
|
// regex for extra tags
|
|
|
|
const spaceregex = /\s*/
|
|
|
|
const notinhtmltagregex = /(?![^<]*>|[^<>]*<\/)/
|
|
|
|
let coloregex = /\[color=([#|(|)|\s|,|\w]*?)\]/
|
|
|
|
coloregex = new RegExp(coloregex.source + notinhtmltagregex.source, 'g')
|
|
|
|
let nameregex = /\[name=(.*?)\]/
|
|
|
|
let timeregex = /\[time=([:|,|+|-|(|)|\s|\w]*?)\]/
|
|
|
|
const nameandtimeregex = new RegExp(nameregex.source + spaceregex.source + timeregex.source + notinhtmltagregex.source, 'g')
|
|
|
|
nameregex = new RegExp(nameregex.source + notinhtmltagregex.source, 'g')
|
|
|
|
timeregex = new RegExp(timeregex.source + notinhtmltagregex.source, 'g')
|
|
|
|
|
|
|
|
function replaceExtraTags (html) {
|
|
|
|
html = html.replace(coloregex, '<span class="color" data-color="$1"></span>')
|
|
|
|
html = html.replace(nameandtimeregex, '<small><i class="fa fa-user"></i> $1 <i class="fa fa-clock-o"></i> $2</small>')
|
|
|
|
html = html.replace(nameregex, '<small><i class="fa fa-user"></i> $1</small>')
|
|
|
|
html = html.replace(timeregex, '<small><i class="fa fa-clock-o"></i> $1</small>')
|
|
|
|
return html
|
2015-09-25 05:41:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// dynamic event or object binding here
|
|
|
|
export function finishView (view) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// todo list
|
2017-03-08 13:41:05 -05:00
|
|
|
const lis = view.find('li.raw').removeClass('raw').sortByDepth().toArray()
|
2017-01-05 04:52:32 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let li of lis) {
|
|
|
|
let html = $(li).clone()[0].innerHTML
|
|
|
|
const p = $(li).children('p')
|
|
|
|
if (p.length === 1) {
|
|
|
|
html = p.html()
|
|
|
|
li = p[0]
|
2015-09-25 05:41:15 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
html = replaceExtraTags(html)
|
|
|
|
li.innerHTML = html
|
|
|
|
let disabled = 'disabled'
|
|
|
|
if (typeof editor !== 'undefined' && window.havePermission()) { disabled = '' }
|
2021-05-05 17:34:03 -04:00
|
|
|
if (/^\s*\[[xX ]]\s*/.test(html)) {
|
|
|
|
li.innerHTML = html.replace(/^\s*\[ ]\s*/, `<input type="checkbox" class="task-list-item-checkbox" ${disabled}><label></label>`)
|
|
|
|
.replace(/^\s*\[[xX]]\s*/, `<input type="checkbox" class="task-list-item-checkbox" checked ${disabled}><label></label>`)
|
2017-03-22 03:42:38 -04:00
|
|
|
if (li.tagName.toLowerCase() !== 'li') {
|
|
|
|
li.parentElement.setAttribute('class', 'task-list-item')
|
|
|
|
} else {
|
|
|
|
li.setAttribute('class', 'task-list-item')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
if (typeof editor !== 'undefined' && window.havePermission()) { $(li).find('input').change(toggleTodoEvent) }
|
2019-05-30 18:27:56 -04:00
|
|
|
// color tag in list will convert it to tag icon with color
|
2017-03-08 13:41:05 -05:00
|
|
|
const tagColor = $(li).closest('ul').find('.color')
|
|
|
|
tagColor.each((key, value) => {
|
|
|
|
$(value).addClass('fa fa-tag').css('color', $(value).attr('data-color'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
// youtube
|
2017-03-08 13:41:05 -05:00
|
|
|
view.find('div.youtube.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.click(function () {
|
2021-08-12 15:14:31 -04:00
|
|
|
imgPlayiframe(this, 'https://www.youtube.com/embed/')
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// vimeo
|
|
|
|
view.find('div.vimeo.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.click(function () {
|
2021-08-12 15:14:31 -04:00
|
|
|
imgPlayiframe(this, 'https://player.vimeo.com/video/')
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
.each((key, value) => {
|
2021-05-15 15:25:03 -04:00
|
|
|
const vimeoLink = `https://vimeo.com/${$(value).attr('data-videoid')}`
|
2019-05-30 18:27:56 -04:00
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2021-05-15 15:25:03 -04:00
|
|
|
url: `https://vimeo.com/api/oembed.json?url=${encodeURIComponent(vimeoLink)}`,
|
2019-05-30 18:27:56 -04:00
|
|
|
jsonp: 'callback',
|
|
|
|
dataType: 'jsonp',
|
|
|
|
success (data) {
|
2021-05-15 15:25:03 -04:00
|
|
|
const image = `<img src="${data.thumbnail_url}" />`
|
2019-05-30 18:27:56 -04:00
|
|
|
$(value).prepend(image)
|
|
|
|
if (window.viewAjaxCallback) window.viewAjaxCallback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// gist
|
|
|
|
view.find('code[data-gist-id]').each((key, value) => {
|
|
|
|
if ($(value).children().length === 0) { $(value).gist(window.viewAjaxCallback) }
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// sequence diagram
|
2017-03-08 13:41:05 -05:00
|
|
|
const sequences = view.find('div.sequence-diagram.raw').removeClass('raw')
|
|
|
|
sequences.each((key, value) => {
|
2021-02-15 03:42:51 -05:00
|
|
|
let $value
|
2017-03-08 13:41:05 -05:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
$value = $(value)
|
2017-03-08 13:41:05 -05:00
|
|
|
const $ele = $(value).parent().parent()
|
|
|
|
|
|
|
|
const sequence = $value
|
|
|
|
sequence.sequenceDiagram({
|
|
|
|
theme: 'simple'
|
|
|
|
})
|
|
|
|
|
|
|
|
$ele.addClass('sequence-diagram')
|
|
|
|
$value.children().unwrap().unwrap()
|
|
|
|
const svg = $ele.find('> svg')
|
|
|
|
svg[0].setAttribute('viewBox', `0 0 ${svg.attr('width')} ${svg.attr('height')}`)
|
|
|
|
svg[0].setAttribute('preserveAspectRatio', 'xMidYMid meet')
|
|
|
|
} catch (err) {
|
|
|
|
$value.unwrap()
|
2019-04-14 12:07:16 -04:00
|
|
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
|
2017-03-08 13:41:05 -05:00
|
|
|
console.warn(err)
|
|
|
|
}
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// flowchart
|
2017-03-08 13:41:05 -05:00
|
|
|
const flow = view.find('div.flow-chart.raw').removeClass('raw')
|
|
|
|
flow.each((key, value) => {
|
2021-02-15 03:42:51 -05:00
|
|
|
let $value
|
2017-03-08 13:41:05 -05:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
$value = $(value)
|
2017-03-08 13:41:05 -05:00
|
|
|
const $ele = $(value).parent().parent()
|
|
|
|
|
|
|
|
const chart = window.flowchart.parse($value.text())
|
|
|
|
$value.html('')
|
|
|
|
chart.drawSVG(value, {
|
|
|
|
'line-width': 2,
|
2021-02-15 03:42:51 -05:00
|
|
|
fill: 'none',
|
2017-03-08 13:41:05 -05:00
|
|
|
'font-size': '16px',
|
|
|
|
'font-family': "'Andale Mono', monospace"
|
|
|
|
})
|
|
|
|
|
|
|
|
$ele.addClass('flow-chart')
|
|
|
|
$value.children().unwrap().unwrap()
|
|
|
|
} catch (err) {
|
|
|
|
$value.unwrap()
|
2019-04-14 12:07:16 -04:00
|
|
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
|
2017-03-08 13:41:05 -05:00
|
|
|
console.warn(err)
|
|
|
|
}
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// graphviz
|
2021-02-15 03:42:51 -05:00
|
|
|
const graphvizs = view.find('div.graphviz.raw').removeClass('raw')
|
2017-03-08 13:41:05 -05:00
|
|
|
graphvizs.each(function (key, value) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let $value
|
2017-03-08 13:41:05 -05:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
$value = $(value)
|
|
|
|
const $ele = $(value).parent().parent()
|
2021-06-06 16:08:27 -04:00
|
|
|
require.ensure([], function (require) {
|
|
|
|
const Viz = require('viz.js')
|
|
|
|
const graphviz = Viz($value.text())
|
|
|
|
if (!graphviz) throw Error('viz.js output empty graph')
|
|
|
|
$value.html(graphviz)
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2021-06-06 16:08:27 -04:00
|
|
|
$ele.addClass('graphviz')
|
|
|
|
$value.children().unwrap().unwrap()
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
} catch (err) {
|
|
|
|
$value.unwrap()
|
2019-04-14 12:07:16 -04:00
|
|
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
|
2017-03-08 13:41:05 -05:00
|
|
|
console.warn(err)
|
|
|
|
}
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// mermaid
|
2017-03-08 13:41:05 -05:00
|
|
|
const mermaids = view.find('div.mermaid.raw').removeClass('raw')
|
|
|
|
mermaids.each((key, value) => {
|
2021-06-06 13:08:06 -04:00
|
|
|
const $value = $(value)
|
|
|
|
const $ele = $(value).closest('pre')
|
|
|
|
require.ensure([], function (require) {
|
|
|
|
try {
|
2021-12-02 13:11:28 -05:00
|
|
|
const mermaid = require('mermaid').default
|
2021-06-06 13:08:06 -04:00
|
|
|
mermaid.startOnLoad = false
|
|
|
|
mermaid.mermaidAPI.parse($value.text())
|
|
|
|
$ele.addClass('mermaid')
|
|
|
|
$ele.text($value.text())
|
|
|
|
mermaid.init(undefined, $ele)
|
|
|
|
} catch (err) {
|
|
|
|
let errormessage = err
|
|
|
|
if (err.str) {
|
|
|
|
errormessage = err.str
|
|
|
|
}
|
|
|
|
$value.unwrap()
|
|
|
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(errormessage)}</div>`)
|
|
|
|
console.warn(errormessage)
|
2017-10-30 06:44:40 -04:00
|
|
|
}
|
2021-06-06 13:08:06 -04:00
|
|
|
})
|
2017-03-26 08:39:07 -04:00
|
|
|
})
|
|
|
|
// abc.js
|
|
|
|
const abcs = view.find('div.abc.raw').removeClass('raw')
|
|
|
|
abcs.each((key, value) => {
|
2021-02-15 03:42:51 -05:00
|
|
|
let $value
|
2017-03-26 08:39:07 -04:00
|
|
|
try {
|
2021-02-15 03:42:51 -05:00
|
|
|
$value = $(value)
|
|
|
|
const $ele = $(value).parent().parent()
|
2021-06-06 14:57:05 -04:00
|
|
|
require.ensure([], function (require) {
|
|
|
|
const abcjs = require('abcjs')
|
|
|
|
abcjs.renderAbc(value, $value.text())
|
|
|
|
$ele.addClass('abc')
|
|
|
|
$value.children().unwrap().unwrap()
|
|
|
|
const svg = $ele.find('> svg')
|
|
|
|
svg[0].setAttribute('viewBox', `0 0 ${svg.attr('width')} ${svg.attr('height')}`)
|
|
|
|
svg[0].setAttribute('preserveAspectRatio', 'xMidYMid meet')
|
|
|
|
})
|
2017-03-26 08:39:07 -04:00
|
|
|
} catch (err) {
|
|
|
|
$value.unwrap()
|
2019-04-14 12:07:16 -04:00
|
|
|
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err)}</div>`)
|
2017-03-26 08:39:07 -04:00
|
|
|
console.warn(err)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// image href new window(emoji not included)
|
2017-03-08 13:41:05 -05:00
|
|
|
const images = view.find('img.raw[src]').removeClass('raw')
|
|
|
|
images.each((key, value) => {
|
2019-05-30 18:27:56 -04:00
|
|
|
// if it's already wrapped by link, then ignore
|
2017-03-08 13:41:05 -05:00
|
|
|
const $value = $(value)
|
|
|
|
$value[0].onload = e => {
|
|
|
|
if (window.viewAjaxCallback) window.viewAjaxCallback()
|
|
|
|
}
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// blockquote
|
2017-03-08 13:41:05 -05:00
|
|
|
const blockquote = view.find('blockquote.raw').removeClass('raw')
|
|
|
|
const blockquoteP = blockquote.find('p')
|
|
|
|
blockquoteP.each((key, value) => {
|
|
|
|
let html = $(value).html()
|
|
|
|
html = replaceExtraTags(html)
|
|
|
|
$(value).html(html)
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// color tag in blockquote will change its left border color
|
2017-03-08 13:41:05 -05:00
|
|
|
const blockquoteColor = blockquote.find('.color')
|
|
|
|
blockquoteColor.each((key, value) => {
|
|
|
|
$(value).closest('blockquote').css('border-left-color', $(value).attr('data-color'))
|
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// slideshare
|
2017-03-08 13:41:05 -05:00
|
|
|
view.find('div.slideshare.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.each((key, value) => {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2021-08-12 15:07:59 -04:00
|
|
|
url: `https://www.slideshare.net/api/oembed/2?url=https://www.slideshare.net/${$(value).attr('data-slideshareid')}&format=json`,
|
2019-05-30 18:27:56 -04:00
|
|
|
jsonp: 'callback',
|
|
|
|
dataType: 'jsonp',
|
|
|
|
success (data) {
|
|
|
|
const $html = $(data.html)
|
|
|
|
const iframe = $html.closest('iframe')
|
|
|
|
const caption = $html.closest('div')
|
|
|
|
const inner = $('<div class="inner"></div>').append(iframe)
|
|
|
|
const height = iframe.attr('height')
|
|
|
|
const width = iframe.attr('width')
|
|
|
|
const ratio = (height / width) * 100
|
|
|
|
inner.css('padding-bottom', `${ratio}%`)
|
|
|
|
$(value).html(inner).append(caption)
|
|
|
|
if (window.viewAjaxCallback) window.viewAjaxCallback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// speakerdeck
|
|
|
|
view.find('div.speakerdeck.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.each((key, value) => {
|
|
|
|
const url = `https://speakerdeck.com/${$(value).attr('data-speakerdeckid')}`
|
|
|
|
const inner = $('<a>Speakerdeck</a>')
|
|
|
|
inner.attr('href', url)
|
|
|
|
inner.attr('rel', 'noopener noreferrer')
|
|
|
|
inner.attr('target', '_blank')
|
|
|
|
$(value).append(inner)
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// pdf
|
|
|
|
view.find('div.pdf.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.each(function (key, value) {
|
|
|
|
const url = $(value).attr('data-pdfurl')
|
|
|
|
const inner = $('<div></div>')
|
|
|
|
$(this).append(inner)
|
|
|
|
PDFObject.embed(url, inner, {
|
|
|
|
height: '400px'
|
|
|
|
})
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// syntax highlighting
|
|
|
|
view.find('code.raw').removeClass('raw')
|
2019-05-30 18:27:56 -04:00
|
|
|
.each((key, value) => {
|
|
|
|
const langDiv = $(value)
|
|
|
|
if (langDiv.length > 0) {
|
|
|
|
const reallang = langDiv[0].className.replace(/hljs|wrap/g, '').trim()
|
2021-06-06 16:08:27 -04:00
|
|
|
if (reallang === 'mermaid' || reallang === 'abc' || reallang === 'graphviz') {
|
2021-06-06 13:08:06 -04:00
|
|
|
return
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
const codeDiv = langDiv.find('.code')
|
|
|
|
let code = ''
|
|
|
|
if (codeDiv.length > 0) code = codeDiv.html()
|
|
|
|
else code = langDiv.html()
|
2021-02-15 03:42:51 -05:00
|
|
|
let result
|
2019-05-30 18:27:56 -04:00
|
|
|
if (!reallang) {
|
|
|
|
result = {
|
|
|
|
value: code
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
} else if (reallang === 'haskell' || reallang === 'go' || reallang === 'typescript' || reallang === 'jsx' || reallang === 'gherkin') {
|
|
|
|
code = S(code).unescapeHTML().s
|
|
|
|
result = {
|
|
|
|
value: Prism.highlight(code, Prism.languages[reallang])
|
|
|
|
}
|
|
|
|
} else if (reallang === 'tiddlywiki' || reallang === 'mediawiki') {
|
|
|
|
code = S(code).unescapeHTML().s
|
|
|
|
result = {
|
|
|
|
value: Prism.highlight(code, Prism.languages.wiki)
|
|
|
|
}
|
|
|
|
} else if (reallang === 'cmake') {
|
|
|
|
code = S(code).unescapeHTML().s
|
|
|
|
result = {
|
|
|
|
value: Prism.highlight(code, Prism.languages.makefile)
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-06 16:20:29 -04:00
|
|
|
require.ensure([], function (require) {
|
|
|
|
const hljs = require('highlight.js')
|
|
|
|
code = S(code).unescapeHTML().s
|
|
|
|
const languages = hljs.listLanguages()
|
|
|
|
if (!languages.includes(reallang)) {
|
|
|
|
result = hljs.highlightAuto(code)
|
|
|
|
} else {
|
|
|
|
result = hljs.highlight(reallang, code)
|
|
|
|
}
|
|
|
|
if (codeDiv.length > 0) codeDiv.html(result.value)
|
|
|
|
else langDiv.html(result.value)
|
|
|
|
})
|
|
|
|
return
|
2019-05-30 18:27:56 -04:00
|
|
|
}
|
|
|
|
if (codeDiv.length > 0) codeDiv.html(result.value)
|
|
|
|
else langDiv.html(result.value)
|
|
|
|
}
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// mathjax
|
|
|
|
const mathjaxdivs = view.find('span.mathjax.raw').removeClass('raw').toArray()
|
|
|
|
try {
|
|
|
|
if (mathjaxdivs.length > 1) {
|
|
|
|
window.MathJax.Hub.Queue(['Typeset', window.MathJax.Hub, mathjaxdivs])
|
|
|
|
window.MathJax.Hub.Queue(window.viewAjaxCallback)
|
|
|
|
} else if (mathjaxdivs.length > 0) {
|
|
|
|
window.MathJax.Hub.Queue(['Typeset', window.MathJax.Hub, mathjaxdivs[0]])
|
|
|
|
window.MathJax.Hub.Queue(window.viewAjaxCallback)
|
2016-11-17 23:19:05 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// render title
|
2017-03-08 13:41:05 -05:00
|
|
|
document.title = renderTitle(view)
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// only static transform should be here
|
|
|
|
export function postProcess (code) {
|
|
|
|
const result = $(`<div>${code}</div>`)
|
2017-10-04 22:17:26 -04:00
|
|
|
// process style tags
|
|
|
|
result.find('style').each((key, value) => {
|
|
|
|
let html = $(value).html()
|
|
|
|
// unescape > symbel inside the style tags
|
|
|
|
html = html.replace(/>/g, '>')
|
|
|
|
// remove css @import to prevent XSS
|
|
|
|
html = html.replace(/@import url\(([^)]*)\);?/gi, '')
|
|
|
|
$(value).html(html)
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// link should open in new window or tab
|
2018-10-03 19:41:48 -04:00
|
|
|
// also add noopener to prevent clickjacking
|
|
|
|
// See details: https://mathiasbynens.github.io/rel-noopener/
|
|
|
|
result.find('a:not([href^="#"]):not([target])').attr('target', '_blank').attr('rel', 'noopener')
|
2021-07-26 16:29:24 -04:00
|
|
|
|
|
|
|
// If it's hashtag link then make it base uri independent
|
|
|
|
result.find('a[href^="#"]').each((index, linkTag) => {
|
|
|
|
const currentLocation = new URL(window.location)
|
|
|
|
currentLocation.hash = linkTag.hash
|
|
|
|
linkTag.href = currentLocation.toString()
|
|
|
|
})
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// update continue line numbers
|
|
|
|
const linenumberdivs = result.find('.gutter.linenumber').toArray()
|
|
|
|
for (let i = 0; i < linenumberdivs.length; i++) {
|
|
|
|
if ($(linenumberdivs[i]).hasClass('continue')) {
|
|
|
|
const startnumber = linenumberdivs[i - 1] ? parseInt($(linenumberdivs[i - 1]).find('> span').last().attr('data-linenumber')) : 0
|
|
|
|
$(linenumberdivs[i]).find('> span').each((key, value) => {
|
|
|
|
$(value).attr('data-linenumber', startnumber + key + 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// show yaml meta paring error
|
|
|
|
if (md.metaError) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let warning = result.find('div#meta-error')
|
2017-03-08 13:41:05 -05:00
|
|
|
if (warning && warning.length > 0) {
|
|
|
|
warning.text(md.metaError)
|
|
|
|
} else {
|
2019-04-14 12:07:16 -04:00
|
|
|
warning = $(`<div id="meta-error" class="alert alert-warning">${escapeHTML(md.metaError)}</div>`)
|
2017-03-08 13:41:05 -05:00
|
|
|
result.prepend(warning)
|
2017-01-04 10:59:28 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
return result
|
2015-09-25 06:18:21 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
window.postProcess = postProcess
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const domevents = Object.getOwnPropertyNames(document).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(Object.getPrototypeOf(document)))).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(window))).filter(function (i) {
|
2017-03-14 04:27:55 -04:00
|
|
|
return !i.indexOf('on') && (document[i] === null || typeof document[i] === 'function')
|
|
|
|
}).filter(function (elem, pos, self) {
|
|
|
|
return self.indexOf(elem) === pos
|
|
|
|
})
|
|
|
|
|
|
|
|
export function removeDOMEvents (view) {
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0, l = domevents.length; i < l; i++) {
|
2017-03-14 04:27:55 -04:00
|
|
|
view.find('[' + domevents[i] + ']').removeAttr(domevents[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.removeDOMEvents = removeDOMEvents
|
|
|
|
|
2021-08-05 15:32:24 -04:00
|
|
|
function toDataURL (url, callback) {
|
|
|
|
fetch(url).then(response => {
|
|
|
|
const fr = new FileReader()
|
|
|
|
fr.onload = function () {
|
|
|
|
callback(this.result)
|
|
|
|
}
|
|
|
|
response.blob().then(blob => {
|
|
|
|
fr.readAsDataURL(blob)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function generateCleanHTML (view) {
|
|
|
|
const src = view.clone()
|
|
|
|
const eles = src.find('*')
|
2019-05-30 18:27:56 -04:00
|
|
|
// remove syncscroll parts
|
2017-03-08 13:41:05 -05:00
|
|
|
eles.removeClass('part')
|
|
|
|
src.find('*[class=""]').removeAttr('class')
|
|
|
|
eles.removeAttr('data-startline data-endline')
|
|
|
|
src.find("a[href^='#'][smoothhashscroll]").removeAttr('smoothhashscroll')
|
2019-05-30 18:27:56 -04:00
|
|
|
// remove gist content
|
2017-03-08 13:41:05 -05:00
|
|
|
src.find('code[data-gist-id]').children().remove()
|
2019-05-30 18:27:56 -04:00
|
|
|
// disable todo list
|
2017-03-08 13:41:05 -05:00
|
|
|
src.find('input.task-list-item-checkbox').attr('disabled', '')
|
2019-05-30 18:27:56 -04:00
|
|
|
// replace emoji image path
|
2017-03-08 13:41:05 -05:00
|
|
|
src.find('img.emoji').each((key, value) => {
|
2021-08-05 15:32:24 -04:00
|
|
|
toDataURL($(value).attr('src'), dataURL => {
|
|
|
|
$(value).attr('src', dataURL)
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
// replace video to iframe
|
2017-03-08 13:41:05 -05:00
|
|
|
src.find('div[data-videoid]').each((key, value) => {
|
|
|
|
const id = $(value).attr('data-videoid')
|
|
|
|
const style = $(value).attr('style')
|
|
|
|
let url = null
|
|
|
|
if ($(value).hasClass('youtube')) {
|
|
|
|
url = 'https://www.youtube.com/embed/'
|
|
|
|
} else if ($(value).hasClass('vimeo')) {
|
|
|
|
url = 'https://player.vimeo.com/video/'
|
|
|
|
}
|
|
|
|
if (url) {
|
|
|
|
const iframe = $('<iframe frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>')
|
|
|
|
iframe.attr('src', url + id)
|
|
|
|
iframe.attr('style', style)
|
|
|
|
$(value).html(iframe)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return src
|
2016-06-17 04:17:37 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function exportToRawHTML (view) {
|
2017-03-28 07:30:06 -04:00
|
|
|
const filename = `${renderFilename(ui.area.markdown)}.html`
|
2017-03-08 13:41:05 -05:00
|
|
|
const src = generateCleanHTML(view)
|
|
|
|
$(src).find('a.anchor').remove()
|
|
|
|
const html = src[0].outerHTML
|
|
|
|
const blob = new Blob([html], {
|
|
|
|
type: 'text/html;charset=utf-8'
|
|
|
|
})
|
|
|
|
saveAs(blob, filename, true)
|
2016-06-17 04:17:37 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// extract markdown body to html and compile to template
|
|
|
|
export function exportToHTML (view) {
|
2017-03-28 07:30:06 -04:00
|
|
|
const title = renderTitle(ui.area.markdown)
|
|
|
|
const filename = `${renderFilename(ui.area.markdown)}.html`
|
2017-03-08 13:41:05 -05:00
|
|
|
const src = generateCleanHTML(view)
|
2019-05-30 18:27:56 -04:00
|
|
|
// generate toc
|
2017-03-08 13:41:05 -05:00
|
|
|
const toc = $('#ui-toc').clone()
|
|
|
|
toc.find('*').removeClass('active').find("a[href^='#'][smoothhashscroll]").removeAttr('smoothhashscroll')
|
|
|
|
const tocAffix = $('#ui-toc-affix').clone()
|
|
|
|
tocAffix.find('*').removeClass('active').find("a[href^='#'][smoothhashscroll]").removeAttr('smoothhashscroll')
|
2019-05-30 18:27:56 -04:00
|
|
|
// generate html via template
|
2021-06-21 14:14:29 -04:00
|
|
|
$.get(`${serverurl}/build/htmlexport.html`, template => {
|
|
|
|
let html = template.replace('{{{url}}}', serverurl)
|
|
|
|
html = html.replace('{{title}}', title)
|
|
|
|
html = html.replace('{{{html}}}', src[0].outerHTML)
|
|
|
|
html = html.replace('{{{ui-toc}}}', toc.html())
|
|
|
|
html = html.replace('{{{ui-toc-affix}}}', tocAffix.html())
|
|
|
|
html = html.replace('{{{lang}}}', (md && md.meta && md.meta.lang) ? `lang="${md.meta.lang}"` : '')
|
|
|
|
html = html.replace('{{{dir}}}', (md && md.meta && md.meta.dir) ? `dir="${md.meta.dir}"` : '')
|
|
|
|
const blob = new Blob([html], {
|
|
|
|
type: 'text/html;charset=utf-8'
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2021-06-21 14:14:29 -04:00
|
|
|
saveAs(blob, filename, true)
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// jQuery sortByDepth
|
2015-07-01 12:10:20 -04:00
|
|
|
$.fn.sortByDepth = function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
const ar = this.map(function () {
|
|
|
|
return {
|
|
|
|
length: $(this).parents().length,
|
|
|
|
elt: this
|
2015-09-25 05:41:15 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}).get()
|
|
|
|
|
|
|
|
const result = []
|
|
|
|
let i = ar.length
|
|
|
|
ar.sort((a, b) => a.length - b.length)
|
|
|
|
while (i--) {
|
|
|
|
result.push(ar[i].elt)
|
|
|
|
}
|
|
|
|
return $(result)
|
2015-09-25 05:41:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function toggleTodoEvent (e) {
|
|
|
|
const startline = $(this).closest('li').attr('data-startline') - 1
|
|
|
|
const line = window.editor.getLine(startline)
|
2021-05-05 17:34:03 -04:00
|
|
|
const matches = line.match(/^[>\s-]*(?:[-+*]|\d+[.)])\s\[([xX ])]/)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (matches && matches.length >= 2) {
|
|
|
|
let checked = null
|
2021-05-05 17:34:03 -04:00
|
|
|
if (matches[1].toLowerCase() === 'x') { checked = true } else if (matches[1] === ' ') { checked = false }
|
|
|
|
const replacements = matches[0].match(/(^[>\s-]*(?:[-+*]|\d+[.)])\s\[)([xX ])(])/)
|
2017-03-08 13:41:05 -05:00
|
|
|
window.editor.replaceRange(checked ? ' ' : 'x', {
|
|
|
|
line: startline,
|
|
|
|
ch: replacements[1].length
|
|
|
|
}, {
|
|
|
|
line: startline,
|
|
|
|
ch: replacements[1].length + 1
|
|
|
|
}, '+input')
|
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// remove hash
|
|
|
|
function removeHash () {
|
|
|
|
history.pushState('', document.title, window.location.pathname + window.location.search)
|
|
|
|
}
|
2016-08-01 22:54:03 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
let tocExpand = false
|
|
|
|
|
|
|
|
function checkExpandToggle () {
|
|
|
|
const toc = $('.ui-toc-dropdown .toc')
|
|
|
|
const toggle = $('.expand-toggle')
|
|
|
|
if (!tocExpand) {
|
|
|
|
toc.removeClass('expand')
|
|
|
|
toggle.text('Expand all')
|
|
|
|
} else {
|
|
|
|
toc.addClass('expand')
|
|
|
|
toggle.text('Collapse all')
|
|
|
|
}
|
2016-08-01 22:54:03 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// toc
|
|
|
|
export function generateToc (id) {
|
|
|
|
const target = $(`#${id}`)
|
|
|
|
target.html('')
|
|
|
|
/* eslint-disable no-unused-vars */
|
2021-02-15 03:42:51 -05:00
|
|
|
const toc = new window.Toc('doc', {
|
|
|
|
level: 3,
|
|
|
|
top: -1,
|
|
|
|
class: 'toc',
|
|
|
|
ulClass: 'nav',
|
|
|
|
targetId: id,
|
|
|
|
process: getHeaderContent
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2017-04-12 14:17:18 -04:00
|
|
|
/* eslint-enable no-unused-vars */
|
2017-03-08 13:41:05 -05:00
|
|
|
if (target.text() === 'undefined') { target.html('') }
|
|
|
|
const tocMenu = $('<div class="toc-menu"></div')
|
|
|
|
const toggle = $('<a class="expand-toggle" href="#">Expand all</a>')
|
|
|
|
const backtotop = $('<a class="back-to-top" href="#">Back to top</a>')
|
|
|
|
const gotobottom = $('<a class="go-to-bottom" href="#">Go to bottom</a>')
|
|
|
|
checkExpandToggle()
|
|
|
|
toggle.click(e => {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
tocExpand = !tocExpand
|
|
|
|
checkExpandToggle()
|
|
|
|
})
|
|
|
|
backtotop.click(e => {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
if (window.scrollToTop) { window.scrollToTop() }
|
|
|
|
removeHash()
|
|
|
|
})
|
|
|
|
gotobottom.click(e => {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
if (window.scrollToBottom) { window.scrollToBottom() }
|
|
|
|
removeHash()
|
|
|
|
})
|
|
|
|
tocMenu.append(toggle).append(backtotop).append(gotobottom)
|
|
|
|
target.append(tocMenu)
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// smooth all hash trigger scrolling
|
|
|
|
export function smoothHashScroll () {
|
|
|
|
const hashElements = $("a[href^='#']:not([smoothhashscroll])").toArray()
|
2017-01-05 04:52:32 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
for (const element of hashElements) {
|
|
|
|
const $element = $(element)
|
|
|
|
const hash = element.hash
|
|
|
|
if (hash) {
|
|
|
|
$element.on('click', function (e) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// store hash
|
2017-03-08 13:41:05 -05:00
|
|
|
const hash = decodeURIComponent(this.hash)
|
2019-05-30 18:27:56 -04:00
|
|
|
// escape special characters in jquery selector
|
2017-03-08 13:41:05 -05:00
|
|
|
const $hash = $(hash.replace(/(:|\.|\[|\]|,)/g, '\\$1'))
|
2019-05-30 18:27:56 -04:00
|
|
|
// return if no element been selected
|
2017-03-08 13:41:05 -05:00
|
|
|
if ($hash.length <= 0) return
|
2019-05-30 18:27:56 -04:00
|
|
|
// prevent default anchor click behavior
|
2017-03-08 13:41:05 -05:00
|
|
|
e.preventDefault()
|
2019-05-30 18:27:56 -04:00
|
|
|
// animate
|
2017-03-08 13:41:05 -05:00
|
|
|
$('body, html').stop(true, true).animate({
|
|
|
|
scrollTop: $hash.offset().top
|
|
|
|
}, 100, 'linear', () => {
|
2019-05-30 18:27:56 -04:00
|
|
|
// when done, add hash to url
|
|
|
|
// (default click behaviour)
|
2017-03-08 13:41:05 -05:00
|
|
|
window.location.hash = hash
|
|
|
|
})
|
|
|
|
})
|
|
|
|
$element.attr('smoothhashscroll', '')
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function imgPlayiframe (element, src) {
|
|
|
|
if (!$(element).attr('data-videoid')) return
|
|
|
|
const iframe = $("<iframe frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>")
|
|
|
|
$(iframe).attr('src', `${src + $(element).attr('data-videoid')}?autoplay=1`)
|
|
|
|
$(element).find('img').css('visibility', 'hidden')
|
|
|
|
$(element).append(iframe)
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-01-05 04:52:32 -05:00
|
|
|
const anchorForId = id => {
|
2017-03-08 13:41:05 -05:00
|
|
|
const anchor = document.createElement('a')
|
|
|
|
anchor.className = 'anchor hidden-xs'
|
|
|
|
anchor.href = `#${id}`
|
2018-10-04 14:01:01 -04:00
|
|
|
anchor.innerHTML = '<i class="fa fa-link"></i>'
|
2017-03-08 13:41:05 -05:00
|
|
|
anchor.title = id
|
|
|
|
return anchor
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2019-10-29 04:58:59 -04:00
|
|
|
const createHeaderId = (headerContent, headerIds = null) => {
|
|
|
|
// to escape characters not allow in css and humanize
|
|
|
|
const slug = slugifyWithUTF8(headerContent)
|
|
|
|
let id
|
|
|
|
if (window.linkifyHeaderStyle === 'keep-case') {
|
|
|
|
id = slug
|
|
|
|
} else if (window.linkifyHeaderStyle === 'lower-case') {
|
|
|
|
// to make compatible with GitHub, GitLab, Pandoc and many more
|
|
|
|
id = slug.toLowerCase()
|
|
|
|
} else if (window.linkifyHeaderStyle === 'gfm') {
|
|
|
|
// see GitHub implementation reference:
|
|
|
|
// https://gist.github.com/asabaylus/3071099#gistcomment-1593627
|
|
|
|
// it works like 'lower-case', but ...
|
2019-10-29 08:20:18 -04:00
|
|
|
const idBase = slug.toLowerCase()
|
|
|
|
id = idBase
|
2019-10-29 04:58:59 -04:00
|
|
|
if (headerIds !== null) {
|
|
|
|
// ... making sure the id is unique
|
|
|
|
let i = 1
|
|
|
|
while (headerIds.has(id)) {
|
2019-10-29 08:20:18 -04:00
|
|
|
id = idBase + '-' + i
|
2019-10-29 04:58:59 -04:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
headerIds.add(id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('Unknown linkifyHeaderStyle value "' + window.linkifyHeaderStyle + '"')
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2017-01-05 04:52:32 -05:00
|
|
|
const linkifyAnchors = (level, containingElement) => {
|
2017-03-08 13:41:05 -05:00
|
|
|
const headers = containingElement.getElementsByTagName(`h${level}`)
|
|
|
|
|
|
|
|
for (let i = 0, l = headers.length; i < l; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const header = headers[i]
|
2017-03-08 13:41:05 -05:00
|
|
|
if (header.getElementsByClassName('anchor').length === 0) {
|
|
|
|
if (typeof header.id === 'undefined' || header.id === '') {
|
2019-10-29 04:58:59 -04:00
|
|
|
header.id = createHeaderId(getHeaderContent(header))
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2018-11-19 12:29:50 -05:00
|
|
|
if (!(typeof header.id === 'undefined' || header.id === '')) {
|
|
|
|
header.insertBefore(anchorForId(header.id), header.firstChild)
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function autoLinkify (view) {
|
|
|
|
const contentBlock = view[0]
|
|
|
|
if (!contentBlock) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for (let level = 1; level <= 6; level++) {
|
|
|
|
linkifyAnchors(level, contentBlock)
|
|
|
|
}
|
2015-09-25 02:27:23 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function getHeaderContent (header) {
|
|
|
|
const headerHTML = $(header).clone()
|
|
|
|
headerHTML.find('.MathJax_Preview').remove()
|
|
|
|
headerHTML.find('.MathJax').remove()
|
|
|
|
return headerHTML[0].innerHTML
|
2016-11-28 09:15:07 -05:00
|
|
|
}
|
|
|
|
|
2019-10-29 04:58:59 -04:00
|
|
|
function changeHeaderId ($header, id, newId) {
|
|
|
|
$header.attr('id', newId)
|
|
|
|
const $headerLink = $header.find(`> a.anchor[href="#${id}"]`)
|
|
|
|
$headerLink.attr('href', `#${newId}`)
|
|
|
|
$headerLink.attr('title', newId)
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function deduplicatedHeaderId (view) {
|
2019-10-29 04:58:59 -04:00
|
|
|
// headers contained in the last change
|
2017-03-08 13:41:05 -05:00
|
|
|
const headers = view.find(':header.raw').removeClass('raw').toArray()
|
2019-10-29 08:20:18 -04:00
|
|
|
if (headers.length === 0) {
|
|
|
|
return
|
2019-10-29 04:58:59 -04:00
|
|
|
}
|
|
|
|
if (window.linkifyHeaderStyle === 'gfm') {
|
|
|
|
// consistent with GitHub, GitLab, Pandoc & co.
|
|
|
|
// all headers contained in the document, in order of appearance
|
2021-02-15 03:42:51 -05:00
|
|
|
const allHeaders = view.find(':header').toArray()
|
2019-10-29 04:58:59 -04:00
|
|
|
// list of finaly assigned header IDs
|
2019-10-29 06:57:51 -04:00
|
|
|
const headerIds = new Set()
|
2019-10-29 04:58:59 -04:00
|
|
|
for (let j = 0; j < allHeaders.length; j++) {
|
|
|
|
const $header = $(allHeaders[j])
|
|
|
|
const id = $header.attr('id')
|
|
|
|
const newId = createHeaderId(getHeaderContent($header), headerIds)
|
|
|
|
changeHeaderId($header, id, newId)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the legacy way
|
|
|
|
for (let i = 0; i < headers.length; i++) {
|
|
|
|
const id = $(headers[i]).attr('id')
|
|
|
|
if (!id) continue
|
|
|
|
const duplicatedHeaders = view.find(`:header[id="${id}"]`).toArray()
|
|
|
|
for (let j = 0; j < duplicatedHeaders.length; j++) {
|
|
|
|
if (duplicatedHeaders[j] !== headers[i]) {
|
|
|
|
const newId = id + j
|
|
|
|
const $header = $(duplicatedHeaders[j])
|
|
|
|
changeHeaderId($header, id, newId)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-09-25 02:27:23 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-09-25 02:27:23 -04:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function renderTOC (view) {
|
|
|
|
const tocs = view.find('.toc').toArray()
|
|
|
|
for (let i = 0; i < tocs.length; i++) {
|
|
|
|
const toc = $(tocs[i])
|
|
|
|
const id = `toc${i}`
|
|
|
|
toc.attr('id', id)
|
|
|
|
const target = $(`#${id}`)
|
|
|
|
target.html('')
|
|
|
|
/* eslint-disable no-unused-vars */
|
2021-02-15 03:42:51 -05:00
|
|
|
const TOC = new window.Toc('doc', {
|
|
|
|
level: 3,
|
|
|
|
top: -1,
|
|
|
|
class: 'toc',
|
|
|
|
targetId: id,
|
|
|
|
process: getHeaderContent
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
if (target.text() === 'undefined') { target.html('') }
|
|
|
|
target.replaceWith(target.html())
|
|
|
|
}
|
2015-12-20 12:28:54 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
export function scrollToHash () {
|
|
|
|
const hash = location.hash
|
|
|
|
location.hash = ''
|
|
|
|
location.hash = hash
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function highlightRender (code, lang) {
|
|
|
|
if (!lang || /no(-?)highlight|plain|text/.test(lang)) { return }
|
|
|
|
code = S(code).escapeHTML().s
|
|
|
|
if (lang === 'sequence') {
|
|
|
|
return `<div class="sequence-diagram raw">${code}</div>`
|
|
|
|
} else if (lang === 'flow') {
|
|
|
|
return `<div class="flow-chart raw">${code}</div>`
|
|
|
|
} else if (lang === 'graphviz') {
|
|
|
|
return `<div class="graphviz raw">${code}</div>`
|
|
|
|
} else if (lang === 'mermaid') {
|
|
|
|
return `<div class="mermaid raw">${code}</div>`
|
2017-03-26 08:39:07 -04:00
|
|
|
} else if (lang === 'abc') {
|
|
|
|
return `<div class="abc raw">${code}</div>`
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
const result = {
|
|
|
|
value: code
|
|
|
|
}
|
|
|
|
const showlinenumbers = /=$|=\d+$|=\+$/.test(lang)
|
|
|
|
if (showlinenumbers) {
|
|
|
|
let startnumber = 1
|
|
|
|
const matches = lang.match(/=(\d+)$/)
|
|
|
|
if (matches) { startnumber = parseInt(matches[1]) }
|
|
|
|
const lines = result.value.split('\n')
|
|
|
|
const linenumbers = []
|
|
|
|
for (let i = 0; i < lines.length - 1; i++) {
|
|
|
|
linenumbers[i] = `<span data-linenumber='${startnumber + i}'></span>`
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
const continuelinenumber = /=\+$/.test(lang)
|
|
|
|
const linegutter = `<div class='gutter linenumber${continuelinenumber ? ' continue' : ''}'>${linenumbers.join('\n')}</div>`
|
|
|
|
result.value = `<div class='wrapper'>${linegutter}<div class='code'>${result.value}</div></div>`
|
|
|
|
}
|
|
|
|
return result.value
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
export const md = markdownit('default', {
|
2017-03-08 13:41:05 -05:00
|
|
|
html: true,
|
|
|
|
breaks: true,
|
|
|
|
langPrefix: '',
|
|
|
|
linkify: true,
|
|
|
|
typographer: true,
|
|
|
|
highlight: highlightRender
|
|
|
|
})
|
|
|
|
window.md = md
|
|
|
|
|
|
|
|
md.use(require('markdown-it-abbr'))
|
|
|
|
md.use(require('markdown-it-footnote'))
|
|
|
|
md.use(require('markdown-it-deflist'))
|
|
|
|
md.use(require('markdown-it-mark'))
|
|
|
|
md.use(require('markdown-it-ins'))
|
|
|
|
md.use(require('markdown-it-sub'))
|
|
|
|
md.use(require('markdown-it-sup'))
|
2016-12-19 14:05:59 -05:00
|
|
|
md.use(require('markdown-it-mathjax')({
|
2017-03-08 13:41:05 -05:00
|
|
|
beforeMath: '<span class="mathjax raw">',
|
|
|
|
afterMath: '</span>',
|
|
|
|
beforeInlineMath: '<span class="mathjax raw">\\(',
|
|
|
|
afterInlineMath: '\\)</span>',
|
|
|
|
beforeDisplayMath: '<span class="mathjax raw">\\[',
|
|
|
|
afterDisplayMath: '\\]</span>'
|
|
|
|
}))
|
|
|
|
md.use(require('markdown-it-imsize'))
|
2016-10-08 08:02:30 -04:00
|
|
|
|
2016-10-18 04:50:58 -04:00
|
|
|
md.use(require('markdown-it-emoji'), {
|
2017-03-08 13:41:05 -05:00
|
|
|
shortcuts: {}
|
|
|
|
})
|
|
|
|
|
|
|
|
window.emojify.setConfig({
|
|
|
|
blacklist: {
|
|
|
|
elements: ['script', 'textarea', 'a', 'pre', 'code', 'svg'],
|
|
|
|
classes: ['no-emojify']
|
|
|
|
},
|
|
|
|
img_dir: `${serverurl}/build/emojify.js/dist/images/basic`,
|
|
|
|
ignore_emoticons: true
|
|
|
|
})
|
|
|
|
|
|
|
|
md.renderer.rules.emoji = (token, idx) => window.emojify.replace(`:${token[idx].markup}:`)
|
|
|
|
|
|
|
|
function renderContainer (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrJoin('role', 'alert')
|
|
|
|
tokens[idx].attrJoin('class', 'alert')
|
|
|
|
tokens[idx].attrJoin('class', `alert-${tokens[idx].info.trim()}`)
|
|
|
|
return self.renderToken(...arguments)
|
2016-03-14 22:56:53 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
md.use(markdownitContainer, 'success', { render: renderContainer })
|
|
|
|
md.use(markdownitContainer, 'info', { render: renderContainer })
|
|
|
|
md.use(markdownitContainer, 'warning', { render: renderContainer })
|
|
|
|
md.use(markdownitContainer, 'danger', { render: renderContainer })
|
2016-02-21 19:58:06 -05:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const defaultImageRender = md.renderer.rules.image
|
2016-02-21 19:58:06 -05:00
|
|
|
md.renderer.rules.image = function (tokens, idx, options, env, self) {
|
2017-03-08 13:41:05 -05:00
|
|
|
tokens[idx].attrJoin('class', 'raw')
|
2018-01-18 11:53:49 -05:00
|
|
|
return defaultImageRender(...arguments)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2016-02-21 19:58:06 -05:00
|
|
|
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
|
2017-03-08 13:41:05 -05:00
|
|
|
tokens[idx].attrJoin('class', 'raw')
|
|
|
|
return self.renderToken(...arguments)
|
|
|
|
}
|
2016-02-21 19:58:06 -05:00
|
|
|
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
|
2017-03-08 13:41:05 -05:00
|
|
|
tokens[idx].attrJoin('class', 'raw')
|
|
|
|
return self.renderToken(...arguments)
|
|
|
|
}
|
2016-02-21 19:58:06 -05:00
|
|
|
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
|
2017-03-08 13:41:05 -05:00
|
|
|
tokens[idx].attrJoin('class', 'raw')
|
|
|
|
return self.renderToken(...arguments)
|
|
|
|
}
|
2017-01-05 04:52:32 -05:00
|
|
|
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
|
2017-03-08 13:41:05 -05:00
|
|
|
const token = tokens[idx]
|
|
|
|
const info = token.info ? md.utils.unescapeAll(token.info).trim() : ''
|
|
|
|
let langName = ''
|
|
|
|
let highlighted
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
langName = info.split(/\s+/g)[0]
|
|
|
|
if (/!$/.test(info)) token.attrJoin('class', 'wrap')
|
|
|
|
token.attrJoin('class', options.langPrefix + langName.replace(/=$|=\d+$|=\+$|!$|=!$/, ''))
|
|
|
|
token.attrJoin('class', 'hljs')
|
|
|
|
token.attrJoin('class', 'raw')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.highlight) {
|
|
|
|
highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content)
|
|
|
|
} else {
|
|
|
|
highlighted = md.utils.escapeHtml(token.content)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highlighted.indexOf('<pre') === 0) {
|
|
|
|
return `${highlighted}\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<pre><code${self.renderAttrs(token)}>${highlighted}</code></pre>\n`
|
|
|
|
}
|
2016-02-21 19:58:06 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// youtube
|
2017-01-05 04:52:32 -05:00
|
|
|
const youtubePlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
2022-08-21 15:57:52 -04:00
|
|
|
/{%youtube\s*([\w-]{11})\s*%}/,
|
2019-05-30 18:27:56 -04:00
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const videoid = match[1]
|
|
|
|
if (!videoid) return
|
|
|
|
const div = $('<div class="youtube raw"></div>')
|
|
|
|
div.attr('data-videoid', videoid)
|
2021-08-12 15:14:31 -04:00
|
|
|
const thumbnailSrc = `https://img.youtube.com/vi/${videoid}/hqdefault.jpg`
|
2019-05-30 18:27:56 -04:00
|
|
|
const image = `<img src="${thumbnailSrc}" />`
|
|
|
|
div.append(image)
|
|
|
|
const icon = '<i class="icon fa fa-youtube-play fa-5x"></i>'
|
|
|
|
div.append(icon)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// vimeo
|
2017-01-05 04:52:32 -05:00
|
|
|
const vimeoPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
2022-08-21 15:57:52 -04:00
|
|
|
/{%vimeo\s*(\d{6,11})\s*%}/,
|
2019-05-30 18:27:56 -04:00
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const videoid = match[1]
|
|
|
|
if (!videoid) return
|
|
|
|
const div = $('<div class="vimeo raw"></div>')
|
|
|
|
div.attr('data-videoid', videoid)
|
|
|
|
const icon = '<i class="icon fa fa-vimeo-square fa-5x"></i>'
|
|
|
|
div.append(icon)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// gist
|
2017-01-05 04:52:32 -05:00
|
|
|
const gistPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
2022-08-21 15:57:52 -04:00
|
|
|
/{%gist\s*(\w+\/\w+)\s*%}/,
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
(match, utils) => {
|
|
|
|
const gistid = match[1]
|
|
|
|
const code = `<code data-gist-id="${gistid}"></code>`
|
|
|
|
return code
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// TOC
|
2017-01-05 04:52:32 -05:00
|
|
|
const tocPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
|
|
|
/^\[TOC\]$/i,
|
2015-12-20 12:28:54 -05:00
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
(match, utils) => '<div class="toc"></div>'
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// slideshare
|
2017-01-05 04:52:32 -05:00
|
|
|
const slidesharePlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
2022-08-21 15:57:52 -04:00
|
|
|
/{%slideshare\s*(\w+\/[\w-]+)\s*%}/,
|
2019-05-30 18:27:56 -04:00
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const slideshareid = match[1]
|
|
|
|
const div = $('<div class="slideshare raw"></div>')
|
|
|
|
div.attr('data-slideshareid', slideshareid)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// speakerdeck
|
2017-01-05 04:52:32 -05:00
|
|
|
const speakerdeckPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
2022-08-21 15:57:52 -04:00
|
|
|
/{%speakerdeck\s*(\w+\/[\w-]+)\s*%}/,
|
2019-05-30 18:27:56 -04:00
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const speakerdeckid = match[1]
|
|
|
|
const div = $('<div class="speakerdeck raw"></div>')
|
|
|
|
div.attr('data-speakerdeckid', speakerdeckid)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
|
|
|
// pdf
|
2017-01-05 04:52:32 -05:00
|
|
|
const pdfPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match
|
|
|
|
/{%pdf\s*([\d\D]*?)\s*%}/,
|
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const pdfurl = match[1]
|
|
|
|
if (!isValidURL(pdfurl)) return match[0]
|
|
|
|
const div = $('<div class="pdf raw"></div>')
|
|
|
|
div.attr('data-pdfurl', pdfurl)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
)
|
2016-01-12 09:01:42 -05:00
|
|
|
|
2018-10-10 15:24:24 -04:00
|
|
|
const emojijsPlugin = new Plugin(
|
2019-05-30 18:27:56 -04:00
|
|
|
// regexp to match emoji shortcodes :something:
|
|
|
|
// We generate an universal regex that guaranteed only contains the
|
|
|
|
// emojies we have available. This should prevent all false-positives
|
|
|
|
new RegExp(':(' + window.emojify.emojiNames.map((item) => { return RegExp.escape(item) }).join('|') + '):', 'i'),
|
|
|
|
|
|
|
|
(match, utils) => {
|
|
|
|
const emoji = match[1].toLowerCase()
|
|
|
|
const div = $(`<img class="emoji" alt=":${emoji}:" src="${serverurl}/build/emojify.js/dist/images/basic/${emoji}.png"></img>`)
|
|
|
|
return div[0].outerHTML
|
|
|
|
}
|
2018-10-10 15:24:24 -04:00
|
|
|
)
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// yaml meta, from https://github.com/eugeneware/remarkable-meta
|
|
|
|
function get (state, line) {
|
|
|
|
const pos = state.bMarks[line]
|
|
|
|
const max = state.eMarks[line]
|
|
|
|
return state.src.substr(pos, max - pos)
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function meta (state, start, end, silent) {
|
|
|
|
if (start !== 0 || state.blkIndent !== 0) return false
|
|
|
|
if (state.tShift[start] < 0) return false
|
|
|
|
if (!get(state, start).match(/^---$/)) return false
|
|
|
|
|
|
|
|
const data = []
|
2021-02-15 03:42:51 -05:00
|
|
|
let line
|
|
|
|
for (line = start + 1; line < end; line++) {
|
2017-03-08 13:41:05 -05:00
|
|
|
const str = get(state, line)
|
|
|
|
if (str.match(/^(\.{3}|-{3})$/)) break
|
|
|
|
if (state.tShift[line] < 0) break
|
|
|
|
data.push(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line >= end) return false
|
|
|
|
|
|
|
|
try {
|
|
|
|
md.meta = window.jsyaml.safeLoad(data.join('\n')) || {}
|
|
|
|
delete md.metaError
|
|
|
|
} catch (err) {
|
|
|
|
md.metaError = err
|
|
|
|
console.warn(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
state.line = line + 1
|
|
|
|
|
|
|
|
return true
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function metaPlugin (md) {
|
|
|
|
md.meta = md.meta || {}
|
|
|
|
md.block.ruler.before('code', 'meta', meta, {
|
|
|
|
alt: []
|
|
|
|
})
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
md.use(metaPlugin)
|
2018-10-10 15:24:24 -04:00
|
|
|
md.use(emojijsPlugin)
|
2017-03-08 13:41:05 -05:00
|
|
|
md.use(youtubePlugin)
|
|
|
|
md.use(vimeoPlugin)
|
|
|
|
md.use(gistPlugin)
|
|
|
|
md.use(tocPlugin)
|
|
|
|
md.use(slidesharePlugin)
|
|
|
|
md.use(speakerdeckPlugin)
|
|
|
|
md.use(pdfPlugin)
|
2016-10-08 08:02:30 -04:00
|
|
|
|
2017-01-05 04:52:32 -05:00
|
|
|
export default {
|
|
|
|
md
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|