This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.babel.js
81 lines (69 loc) · 2.53 KB
/
gulpfile.babel.js
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
import gulp from 'gulp'
import gutil from 'gulp-util'
import tap from 'gulp-tap'
import gap from 'gulp-append-prepend'
import rewriteImgPath from 'gulp-rewrite-image-path'
import replace from 'gulp-replace'
import MarkdownIt from 'markdown-it'
import mdImSize from 'markdown-it-imsize'
import mdAdmonition from 'markdown-it-admonition'
import mdTocAndAnchor from 'markdown-it-toc-and-anchor'
import productionLinks from './src/productionLinks.js'
import path from 'path'
const MARKDOWN_GLOB = 'src/**/*.md'
let md = new MarkdownIt({ html: true })
.use(mdImSize, { autofill: true, html: true, typographer: true })
.use(mdAdmonition)
.use(mdTocAndAnchor, { tocFirstLevel: 2, tocLastLevel: 3, anchorLink: false })
function _markdownToHtml(file) {
let result = md.render(file.contents.toString())
file.contents = new Buffer.from(result)
file.path = gutil.replaceExtension(file.path, '.html')
}
function build() {
return gulp.src(MARKDOWN_GLOB)
.pipe(replace(/\*\*Title\:\*\*.*/g, ''))
.pipe(replace(/\[(.*)\]\: (.*)/g, function(match, key, file) {
let anchor = '';
if (file.indexOf('#') != -1) {
anchor = file.substr(file.indexOf('#'));
file = file.substr(0, file.indexOf('#'));
}
if (file.length == 0 && anchor.length > 0) return match; // is a reference on the same file
if (file.startsWith('https://') || file.startsWith('http://')) return match; // is a complete URL
let dest = path.normalize(path.join(path.dirname(this.file.relative), file)).replace(/\\/g, '/');
if (dest in productionLinks) {
return `[${key}]: ${productionLinks[dest]}${anchor}`;
} else {
throw Error(`No production link found for "${dest}" in src/productionLinks.js - please update (matched ${match} in ${this.file.relative})`);
}
}))
.pipe(tap(_markdownToHtml))
.pipe(gap.prependFile('src/header.html'))
.pipe(gap.prependFile('src/footer.html'))
.pipe(rewriteImgPath({ path: 'https://about.signpath.io/wp-content/uploads' }))
.pipe(gulp.dest('build'));
}
function buildLocal() {
return gulp.src(MARKDOWN_GLOB)
.pipe(tap(_markdownToHtml))
.pipe(gap.prependFile('src/local/header.html'))
.pipe(gap.prependFile('src/local/footer.html'))
.pipe(rewriteImgPath({ path: '../../src/images' }))
.pipe(gulp.dest('buildLocal'))
}
function watchLocal() {
gulp.watch(MARKDOWN_GLOB, buildLocal)
}
function watch() {
gulp.watch(MARKDOWN_GLOB, build)
}
export default build;
export {
build,
buildLocal,
watch,
watchLocal
};
// TODO
// * check if file exists