-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathdocpad.coffee
64 lines (52 loc) · 1.68 KB
/
docpad.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
path = require 'path'
safeps = require 'safeps'
hljs = require './plugins/highlight.js'
docpadConfig = {
gulpArgs: ['html']
templateData:
site:
author: "Sergey Chikuyonok"
name: "Emmet Docs & Tutorials"
plugins:
marked:
markedOptions:
sanitize: false
highlight: (text, lang) ->
result = if lang then hljs.highlight(lang, text) else hljs.highlightAuto(text)
"<span class=\"#{result.language}\">#{result.value}</span>"
environments:
production:
gulpArgs: ['full', '--production']
events:
# Extend server so it can respond to cache-reset assets
serverAfter: ({server}) ->
reCache = /^\/-\/.+?\//
server.get reCache, (req, res, next) ->
req.url = req.url.replace reCache, '/'
next()
# Supply headers with named anchors
renderDocument: (opts) ->
{extension,file} = opts
if file.type is 'document' and extension is 'html'
opts.content = opts.content.replace /<(h\d)(.*?)>(.+?)<\/\1>/g, (str, name, attrs, header) ->
if /<a\s+[^>]*name="/.test(header)
return str
# strip tags
# console.log header
anchor = header.replace /<\/?\w+(?:\s.+?)*>/g, '';
anchor = anchor
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]/g, '')
.replace(/\-+$/g, '')
.toLowerCase()
"<#{name + attrs}><a name=\"#{anchor}\" href=\"\##{anchor}\" class=\"anchor\"></a>#{header}</#{name}>"
writeAfter: (opts, next) ->
config = @docpad.getConfig()
rootPath = config.rootPath
gulpPath = path.join(rootPath, 'node_modules', '.bin', 'gulp')
command = [gulpPath].concat(config.gulpArgs or [])
safeps.spawn(command, {cwd: rootPath, output: true}, next)
@
}
module.exports = docpadConfig