forked from atom/markdown-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.coffee
148 lines (121 loc) · 4.69 KB
/
renderer.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
path = require 'path'
_ = require 'underscore-plus'
cheerio = require 'cheerio'
fs = require 'fs-plus'
Highlights = require 'highlights'
{$} = require 'atom-space-pen-views'
roaster = null # Defer until used
{scopeForFenceName} = require './extension-helper'
highlighter = null
{resourcePath} = atom.getLoadSettings()
packagePath = path.dirname(__dirname)
exports.toDOMFragment = (text='', filePath, grammar, callback) ->
render text, filePath, (error, html) ->
return callback(error) if error?
template = document.createElement('template')
template.innerHTML = html
domFragment = template.content.cloneNode(true)
# Default code blocks to be coffee in Literate CoffeeScript files
defaultCodeLanguage = 'coffee' if grammar?.scopeName is 'source.litcoffee'
convertCodeBlocksToAtomEditors(domFragment, defaultCodeLanguage)
callback(null, domFragment)
exports.toHTML = (text='', filePath, grammar, callback) ->
render text, filePath, (error, html) ->
return callback(error) if error?
# Default code blocks to be coffee in Literate CoffeeScript files
defaultCodeLanguage = 'coffee' if grammar?.scopeName is 'source.litcoffee'
html = tokenizeCodeBlocks(html, defaultCodeLanguage)
callback(null, html)
render = (text, filePath, callback) ->
roaster ?= require 'roaster'
options =
sanitize: false
breaks: atom.config.get('markdown-preview.breakOnSingleNewline')
# Remove the <!doctype> since otherwise marked will escape it
# https://github.com/chjj/marked/issues/354
text = text.replace(/^\s*<!doctype(\s+.*)?>\s*/i, '')
roaster text, options, (error, html) ->
return callback(error) if error?
html = sanitize(html)
html = resolveImagePaths(html, filePath)
callback(null, html.trim())
sanitize = (html) ->
o = cheerio.load(html)
o('script').remove()
attributesToRemove = [
'onabort'
'onblur'
'onchange'
'onclick'
'ondbclick'
'onerror'
'onfocus'
'onkeydown'
'onkeypress'
'onkeyup'
'onload'
'onmousedown'
'onmousemove'
'onmouseover'
'onmouseout'
'onmouseup'
'onreset'
'onresize'
'onscroll'
'onselect'
'onsubmit'
'onunload'
]
o('*').removeAttr(attribute) for attribute in attributesToRemove
o.html()
resolveImagePaths = (html, filePath) ->
[rootDirectory] = atom.project.relativizePath(filePath)
o = cheerio.load(html)
for imgElement in o('img')
img = o(imgElement)
if src = img.attr('src')
continue if src.match(/^(https?|atom):\/\//)
continue if src.startsWith(process.resourcesPath)
continue if src.startsWith(resourcePath)
continue if src.startsWith(packagePath)
if src[0] is '/'
unless fs.isFileSync(src)
img.attr('src', path.join(rootDirectory, src.substring(1)))
else
img.attr('src', path.resolve(path.dirname(filePath), src))
o.html()
convertCodeBlocksToAtomEditors = (domFragment, defaultLanguage='text') ->
if fontFamily = atom.config.get('editor.fontFamily')
for codeElement in domFragment.querySelectorAll('code')
codeElement.style.fontFamily = fontFamily
for preElement in domFragment.querySelectorAll('pre')
codeBlock = preElement.firstElementChild ? preElement
fenceName = codeBlock.getAttribute('class')?.replace(/^lang-/, '') ? defaultLanguage
editorElement = document.createElement('atom-text-editor')
editorElement.setAttributeNode(document.createAttribute('gutter-hidden'))
editorElement.removeAttribute('tabindex') # make read-only
preElement.parentNode.insertBefore(editorElement, preElement)
preElement.remove()
editor = editorElement.getModel()
# remove the default selection of a line in each editor
editor.getDecorations(class: 'cursor-line', type: 'line')[0].destroy()
editor.setText(codeBlock.textContent.trim())
if grammar = atom.grammars.grammarForScopeName(scopeForFenceName(fenceName))
editor.setGrammar(grammar)
domFragment
tokenizeCodeBlocks = (html, defaultLanguage='text') ->
o = cheerio.load(html)
if fontFamily = atom.config.get('editor.fontFamily')
o('code').css('font-family', fontFamily)
for preElement in o("pre")
codeBlock = o(preElement).children().first()
fenceName = codeBlock.attr('class')?.replace(/^lang-/, '') ? defaultLanguage
highlighter ?= new Highlights(registry: atom.grammars)
highlightedHtml = highlighter.highlightSync
fileContents: codeBlock.text()
scopeName: scopeForFenceName(fenceName)
highlightedBlock = o(highlightedHtml)
# The `editor` class messes things up as `.editor` has absolutely positioned lines
highlightedBlock.removeClass('editor').addClass("lang-#{fenceName}")
o(preElement).replaceWith(highlightedBlock)
o.html()