-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparser.js
82 lines (72 loc) · 2.37 KB
/
parser.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
82
// Copyright 2020 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.
import yaml from 'js-yaml';
import showdown from 'showdown';
import cheerio from 'cheerio';
import ReactParser from 'html-react-parser';
import Prism from 'prismjs';
import 'prismjs/components/prism-clike'
import 'prismjs/components/prism-java'
import 'prismjs/themes/prism.css'
// parse yaml
export const yamlToJson = (content) => (
yaml.safeLoad(content)
);
// parse md file
export const md = (content) => {
const converter = new showdown.Converter({
tables: true,
strikethrough: true,
ghCompatibleHeaderId: true,
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
ghCodeBlocks: true,
ghMentions: true,
ghMentionsLink: true,
excludeTrailingPunctuationFromURLs: true,
});
// return HTML String
return converter.makeHtml(content);
};
export const parsePlatformData = (content) => {
const $ = cheerio.load(content, {
xmlMode: true
});
$('h1').addClass('header-1');
$('h2').addClass('header-2 ');
$('p').addClass('para');
$('ul').addClass('list');
$('li').addClass('list-i');
$('a').map((i, ele) => {
const href = $(ele).attr('href');
if (href && href[0] === '#') {
return;
}
$(ele).attr('target', '_blank');
});
$('pre').map((index, element) => {
const ele = $(element);
const codeEle = ele.children('code');
const code = codeEle.contents()[0].data;
const highlightedHtml = Prism.highlight(code, Prism.languages.javascript, 'javascript');
ele.addClass('language-javascript')
codeEle.html(highlightedHtml);
}).get().join(' ');
$('li code').map((i, ele) => {
$(ele).addClass('language-basic')
}).get().join(' ');
$('p code').map((i, ele) => {
$(ele).addClass('language-basic')
}).get().join(' ');
const navEle = $.root().children().filter((i, el) => ['h1', 'h2', 'h3'].indexOf(el.name) !== -1);
return {
html: ReactParser($.html()),
nav: navEle
};
};