forked from smazanek/markdown-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (41 loc) · 1.36 KB
/
index.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
const chunk = require('lodash.chunk');
const flatten = require('lodash.flatten');
const fs = require('fs');
const {parseToTree, getTextTobeTranslated, stringifyToDoc} = require('./lib/parseMarkdown');
const {translate} = require('./lib/translateByAWS');
module.exports = ({
src, from, to, subscriptionKey, region
}) => {
return new Promise((resolve, reject) => {
console.log('Starting translate process from '+ from +' to '+to);
const tree = parseToTree(src);
const nodeArr = getTextTobeTranslated(tree);
const textArr = nodeArr.reduce((prev = [], cur) => {
if(cur && cur.value) {
prev.push({
text: cur.value
});
}
return prev;
}, []);
const translatePromises = []
for (let eachText of textArr) {
translatePromises.push(translate(eachText['text'], {
from, to, subscriptionKey, region
}));
}
Promise.all(translatePromises).then(data => {
//let data = flatten(dataArr);
for (let node of nodeArr) {
if(node && node.value) {
const result = data.shift();
if (result && result['TranslatedText']) {
console.log ('text: ' + node.value + ' => ' + result['TranslatedText']);
node.value = result['TranslatedText'];
}
}
}
resolve(stringifyToDoc(tree));
}).catch(reject);
})
}