-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringHelper.js
126 lines (118 loc) · 3.53 KB
/
StringHelper.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
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
const HTML = require('./node_modules/html-parse-stringify/index.js');
const FONTSTYLE = {
BOLD: 'Bold',
ITALIC: 'Italic',
NORMAL: 'Normal'
};
const TEXTALIGN = {
LEFT: 'Left',
CENTER: 'Center',
RIGHT: 'Right'
};
function stripHTML(text) {
// remove wordpress shortcode , credit: http://stackoverflow.com/questions/20281294/finding-and-removing-all-occurences-of-shortcode-with-javascript
text = text.replace(/\[.*?\]/g, "");
// remove html element
return text.replace(/<.*?>/gm, '');
}
function stripImage(text) {
// remove <img /> element from text. credit: var content = content.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g,"");
return text.replace(/<img[^>"']*((("[^"]*")|('[^']*'))[^"'>]*)*>/g, "");
}
function renderToHtml(content) {
return `
<html>
<head>
<title>Lakon Hidup</title>
<style>
body { background: yellow }
p, span{ font-size: 40px; }
</style>
</head>
<body>
${content}
</body>
</html>
`;
}
/**
* parse paragraph using html-parse-stringify node_modules
* @param {text} text to be parsed
* @param {paragraphNumber} paragraph number, only for indexing
*
* @return {
* alignment: alignment of a paragraphs
* wordsChunk: chunk of Word Object {type: 'type of object', content: 'content of a words'}
* }
*/
function parseParagraph(text, paragraphNumber) {
var results = [];
var alignment = 'Left';
// strip image first
text = stripImage(text);
// becomes this AST:
var ast = HTML.parse(text);
// console.log(text);
if (ast.length > 0) {
console.log('paragraphNumber : ' + paragraphNumber);
if (ast[0].children.length > 0) {
console.log(`type: ${ast[0].type} -- name: ${ast[0].name}`);
ast[0].children.forEach(function(val, key) {
let textObject = {
content: '',
fontStyle: ''
}
if (val.type == 'text') {
textObject = {
content: val.content,
fontStyle: FONTSTYLE.NORMAL
}
// results.push({ type: FONTSTYLE.NORMAL, content: textInside });
} else if (val.type == 'tag') {
let textInside = '';
val.children.forEach(function(val2, key2) {
textInside += val2.content + ' ';
})
if (val.name == 'em') {
// results.push({ type: FONTSTYLE.ITALIC, content: textInside });
textObject = {
content: textInside,
fontStyle: FONTSTYLE.ITALIC
}
} else {
// results.push({ type: FONTSTYLE.BOLD, content: textInside });
textObject = {
content: textInside,
fontStyle: FONTSTYLE.BOLD
}
}
}
results.push(textObject);
console.log(`---> type: ${textObject.fontStyle} -- name: ${val.name} -- content: ${textObject.content}`);
});
}
if (ast[0].attrs.style) {
console.log(JSON.stringify(ast[0].attrs.style));
let style = ast[0].attrs.style.split('text-align:');
if (style.length > 1) {
if (style[1].indexOf('left') > -1) {
alignment = TEXTALIGN.LEFT;
} else if (style[1].indexOf('right') > -1) {
alignment = TEXTALIGN.RIGHT;
} else if (style[1].indexOf('center') > -1) {
alignment = TEXTALIGN.CENTER;
}
}
}
}
return {
alignment: alignment,
wordChunks: results
};
}
module.exports = {
stripHTML: stripHTML,
stripImage: stripImage,
renderToHtml: renderToHtml,
parseParagraph: parseParagraph
}