-
Notifications
You must be signed in to change notification settings - Fork 21
/
docpad.coffee
204 lines (176 loc) · 7.39 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# DocPad Configuration File
# http://docpad.org/docs/config
moment = require 'moment'
fs = require 'fs'
# Define the DocPad Configuration
docpadConfig = {
plugins:
cleanurls:
trailingSlashes: true
rss:
default:
collection: 'articles'
grunt:
writeAfter: false
generateAfter: ["shell", "copy"]
ghpages:
deployRemote: 'origin'
deployBranch: 'master'
moment:
formats: [
{raw: 'date', format: 'Do MMMM YYYY', formatted: 'humanDate'}
{raw: 'date', format: 'YYYY-MM-DD', formatted: 'computerDate'}
]
marked:
markedRenderer:
heading: (text, level)->
anchor = text.toLowerCase().replace(/[^\w]+/g, '-');
anchor = text.replace /<\/?\w+(?:\s.+?)*>/g, '';
anchor = anchor
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]/g, '')
.toLowerCase()
"<h#{level} class=\"text-header text-header_lvl_#{level}\" id=\"#{anchor}\"><a href=\"\##{anchor}\" class=\"text-header__anchor\">#{text}</a></h#{level}>"
paragraph: (text) ->
"<p class=\"text__p\">#{text}</p>"
image: (src) ->
"<img class=\"text-image i-bem\" data-bem='{ \"text-image\": {} }' src=\"#{src}\"/>"
code: (text, lang) ->
params = "{}"
lang && params = "{ \"lang\": \"#{lang}\"}"
"<pre class=\"highlight i-bem\" data-bem='{ \"highlight\": #{params} }'><code>#{text}</code></pre>"
table: (header, body) ->
"<table class=\"text-table\">" +
"<thead class=\"text-table__head\">#{header}</thead>" +
"<tbody class=\"text-table__body\">#{body}</tbody>" +
"</table>"
tablerow: (content) ->
"<tr class=\"text-table__row\">#{content}</tr>"
tablecell: (content, flags) ->
type = if flags.header then 'th' else 'td'
klass = if flags.header then 'text-table__th' else 'text-table__td'
if flags.align
tag = "<#{type} class=\"#{klass}\" style=\"text-align: #{flags.align}\">"
else
tag = "<#{type} class=\"#{klass}\">"
"#{tag}#{content}</#{type}>"
templateData:
site:
title: 'Frontend Babel'
description: 'Articles and blog posts from all over the world. Translated INTO English.'
services:
disqus: 'frontendbabel'
url: 'http://frontendbabel.info'
author: 'Frontend Babel'
cutTag: '<!-- cut -->'
# Post part before “cut”
cuttedContent: (content) ->
if @hasReadMore content
cutIdx = content.search @cutTag
content[0..cutIdx-1]
else
content
# Has “cut”?
hasReadMore: (content) ->
content and ((content.search @cutTag) isnt -1)
hlp:
pageTitle: (document) ->
switch document.layout
when 'index' then "Frontend Babel. Articles and blog posts from all over the world"
else "#{document.title} — Frontend Babel"
personLink: (person) ->
if person.site
"<a href=\"#{person.site}\">#{person.name}</a>"
else
person.name
articleCredits: (article) ->
"Written by #{@personLink(article.authorInfo)}#{@onDate(article.source.date)}; " +
"translated by #{@personLink(article.translator)}#{@onDate(article.date)}"
onDate: (date) ->
if date
" on " + moment(date).format('Do MMMM YYYY')
else
""
metaProps: (document) ->
if document.meta.layout == 'index'
desc = "Frontend Babel — an online hub for publishing English translations of frontend articles originally written in other languages. Not all authors have time, resources or skills to make an English version of what they write. Other members of the community can change that, contributing their translations and helping the world discover new frontend stars, experts, and innovators. Across boundaries."
if document.meta.desc
desc = document.meta.desc
if document.thumb
thumb = document.thumb
"" +
(if desc then "<meta content=\"#{desc}\" property=\"og:description\"/>" else "") +
(if thumb then "<meta content=\"#{thumb}\" property=\"og:image\"/>" else "")
events:
renderBefore: () ->
this.docpad.getCollection('html').forEach (document) ->
a = document.attributes
# Use document's base file name as a default title
if a.title == null
title = a.basename.replace(/-/g, ' ')
title = title.charAt(0).toUpperCase() + title.slice(1);
document.set('title', title)
# Hardcode the menu order for the contributing guideline
# TODO: there should be a better wat to do this
if a.basename == 'how-to-contribute' && a.order == undefined
document.set('order', 3)
collections:
pages: (database) ->
@getCollection('documents').findAllLive({ relativeOutDirPath: 'pages' }).on 'add', (document) ->
a = document.attributes
basename = a.basename
newUrl = "#{basename}.#{a.outExtension}"
document.set('isPage', true)
urls = ["/#{newUrl}"]
layout = a.layout
if !layout
layout = 'page'
document
.setMetaDefaults({
url: urls[0]
layout: layout
})
.addUrl(urls)
menu: (database) ->
@getCollection('pages').findAllLive({ order: $ne: false })
articles: (database) ->
@getCollection('documents').findAllLive({ relativeOutBase: /^articles\//, extension: 'md' }).on 'add', (document)->
a = document.attributes
layout = a.layout
if !layout
layout = 'article'
document
.setMetaDefaults({
layout: layout
})
checkThumb = (path) ->
exts = ['jpeg', 'png', 'jpg', 'gif']
for k, ext of exts
if fs.existsSync "#{path}/thumb.#{ext}"
return "thumb.#{ext}"
# Check thumbnail
thumb = checkThumb(['src/documents', a.relativeDirPath].join('/'))
if thumb
thumb = "http://frontendbabel.info/#{a.relativeOutDirPath}/#{thumb}"
if !a.thumb
a.thumb = thumb
repo = {
owner: 'frontendbabel'
name: 'frontendbabel.github.com'
branch: 'source'
}
author = a.author || {}
# Normalize meta info
document.set({
author: author.name
authorInfo: author
source: a.source || {}
image: a.thumb || ""
translator: a.translator || {}
sourcePath: ['https://github.com', repo.owner, repo.name, 'edit', repo.branch, 'src/documents', a.relativePath].join('/')
})
env: 'static'
}
# Export the DocPad Configuration
module.exports = docpadConfig