1 |
|
2 | 'use strict'
|
3 |
|
4 | const Language = require('./language')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | let Mdx = {}
|
13 | module.exports = Mdx
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | Mdx.detectLanguage = function (fname) {
|
23 | for (let name in this.languages) {
|
24 | let lang = Mdx.getLanguage(name)
|
25 | if (lang.matches(fname)) return lang
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | Mdx.getLanguage = function (name) {
|
36 | let lang = this.languages[name]
|
37 | return lang && new Language(name, lang)
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | Mdx.languages = require('./config').languages
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Mdx.Extractor = require('./extractor')
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Mdx.renderTemplate = function (output) {
|
62 | const mustache = require('mustache')
|
63 | const tplFile = require.resolve('../templates/markdown.mustache')
|
64 | const fs = require('fs')
|
65 | const tpl = fs.readFileSync(tplFile, 'utf-8')
|
66 |
|
67 |
|
68 | output = JSON.parse(JSON.stringify(output))
|
69 |
|
70 | output.blocks.forEach(function (block) {
|
71 | if (~['class', 'module', 'section'].indexOf(block.type)) {
|
72 | block._headerprefix = '##'
|
73 | } else {
|
74 | block._headerprefix = '###'
|
75 | }
|
76 | })
|
77 |
|
78 | return mustache.render(tpl, output).trim() + '\n'
|
79 | }
|