-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
75 lines (66 loc) · 2.49 KB
/
gulpfile.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
const fs = require('fs')
const showdown = require('showdown')
const buildOutput = 'dist'
const build = function () {
return new Promise(( resolve, reject ) => {
// Convert showcase description markdown to HTML understandable format, and include in build.
const source = "./README.md"
const target = `${buildOutput}/description.md`
fs.readFile( source, (err, buffer) => {
if ( err )
reject( err )
let md = buffer.toString()
// Grab very first line of markdown as "Title".
const title = md.split('\n')[0]
// Filter everything out except "Description" chapter.
md = title + '\n' + getChapterFromMarkdown(
md,
/^#+\s+Description/,
)
const converter = new showdown.Converter()
converter.setFlavor( 'github' )
const htmlMd = converter.makeHtml( md )
// Write to build output.
fs.writeFile( target, htmlMd, (err) => {
if ( err )
reject( err )
resolve()
})
})
})
}
/**
* Get a single chapter from markdown.
* @param {string} input Markdown string.
* @param {RegExp} regExp Regular expression to look for chapter.
* @returns {string} Stripped markdown string.
*/
const getChapterFromMarkdown = (input, regExp) => {
// Split the input into a easily manageable array.
let splitInput = input.split('\n')
let iChapterStart = splitInput.findIndex(( row ) =>
regExp.test( row )
)
// Count amount of #'s in chapter declaration.
const chapterDepth = 1 + splitInput[ iChapterStart ].lastIndexOf( '#' )
// Find index of next Chapter with same or lesser amount of #'s.
let iChapterEnd = -1 + splitInput.findIndex(( row, i ) => {
if ( i <= iChapterStart )
return false
for ( let iChar = 0; iChar < row.length; iChar ++ ) {
if ( row.charAt( iChar ) !== '#' ) {
if ( iChar > 0 && iChar <= chapterDepth ) {
return true
} else
return false
}
}
return false
})
// Remove empty lines from stripped chapter.
iChapterStart += 1
iChapterEnd -= 1
// Join lines of Chapter.
return splitInput.filter((_, i) => i > iChapterStart && i <= iChapterEnd).join('\n')
}
exports.build = build