Export a zipped file of the translations on a project
This commit is contained in:
@@ -1,41 +1,12 @@
|
||||
const express = require('express')
|
||||
const { Op } = require("sequelize")
|
||||
const idGenerator = require('mantra-db-models/src/lib/id-generator');
|
||||
const Token = require('markdown-it/lib/token');
|
||||
const md = require("markdown-it")().disable(['link'])
|
||||
const markdownProducer = require("../../../lib/markdown-producer")
|
||||
|
||||
module.exports = function (options) {
|
||||
const db = options.db;
|
||||
var router = express.Router();
|
||||
|
||||
const produceTranslatedChapterOutput = (translationChapter) => {
|
||||
return md.parse(
|
||||
translationChapter.chapter.originalText
|
||||
).map(token => {
|
||||
if (token.content) {
|
||||
const translationChunk = translationChapter.translationChunks.find(tc => {
|
||||
return tc.text == token.content
|
||||
})
|
||||
|
||||
if (translationChunk) {
|
||||
token.content = translationChunk.translation.text
|
||||
if (token.children.length == 1) {
|
||||
token.children[0].content = translationChunk.translation.text
|
||||
} else {
|
||||
const newChild = new Token(
|
||||
"text"
|
||||
)
|
||||
newChild.content = translationChunk.translation.text
|
||||
token.children = [
|
||||
newChild
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return token
|
||||
})
|
||||
}
|
||||
router.route('/:id')
|
||||
.get(function(request, response, next) {
|
||||
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
||||
@@ -249,16 +220,11 @@ module.exports = function (options) {
|
||||
]
|
||||
}).then(translationChapter => {
|
||||
if (translationChapter) {
|
||||
const tokens = produceTranslatedChapterOutput(translationChapter)
|
||||
// TODO: Make markdown string...
|
||||
|
||||
|
||||
response.display("translate-chapter-view", {
|
||||
user: request.user,
|
||||
pageTitle: `Translate Chapter ${translationChapter.name}`,
|
||||
translatedChapterOutput: md.renderer.render(
|
||||
tokens,
|
||||
{}
|
||||
),
|
||||
translatedChapterOutput: markdownProducer.produceHtml(translationChapter),
|
||||
translationChapter: translationChapter
|
||||
})
|
||||
} else {
|
||||
@@ -306,16 +272,8 @@ module.exports = function (options) {
|
||||
]
|
||||
}).then(translationChapter => {
|
||||
if (translationChapter) {
|
||||
// TODO: Parse markdown...
|
||||
const tokens = produceTranslatedChapterOutput(translationChapter)
|
||||
// TODO: Make markdown string...
|
||||
|
||||
const translatedChapterContent = md.renderer.render(
|
||||
tokens,
|
||||
{}
|
||||
)
|
||||
response.contentType = "text/plain"
|
||||
response.send(translatedChapterContent)
|
||||
response.send(markdownProducer.produceHtml(translationChapter))
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
@@ -361,54 +319,8 @@ module.exports = function (options) {
|
||||
]
|
||||
}).then(translationChapter => {
|
||||
if (translationChapter) {
|
||||
// TODO: Parse markdown...
|
||||
const tokens = produceTranslatedChapterOutput(translationChapter)
|
||||
// TODO: Make markdown string...
|
||||
// collect tokens...
|
||||
response.contentType = "text/plain"
|
||||
response.send(tokens.reduce(
|
||||
(accumulator, token) => {
|
||||
if (token.type == "list_item_close") {
|
||||
return accumulator
|
||||
}
|
||||
|
||||
if (token.type == "ordered_list_open" || token.type == "bullet_list_open") {
|
||||
return accumulator
|
||||
}
|
||||
|
||||
if (token.type == "bullet_list_close" || token.type == "ordered_list_close") {
|
||||
return accumulator + "\n"
|
||||
}
|
||||
|
||||
if (token.type == "paragraph_close") {
|
||||
if (token.level == 0) {
|
||||
return accumulator + "\n\n"
|
||||
} else {
|
||||
return accumulator + "\n"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (token.type == "list_item_open") {
|
||||
return accumulator + `${token.info}${token.markup} `
|
||||
}
|
||||
|
||||
if (token.type == "inline") {
|
||||
return accumulator + token.content
|
||||
}
|
||||
|
||||
if (token.type == "heading_open") {
|
||||
return `${accumulator}${token.markup} `
|
||||
}
|
||||
|
||||
if (token.type == "heading_close") {
|
||||
return `${accumulator}\n`
|
||||
}
|
||||
|
||||
return accumulator + token.markup
|
||||
},
|
||||
""
|
||||
))
|
||||
response.send(markdownProducer.produceMarkdownString(tokens))
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user