Output markdown
This commit is contained in:
@@ -1,11 +1,41 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const { Op } = require("sequelize")
|
const { Op } = require("sequelize")
|
||||||
const idGenerator = require('mantra-db-models/src/lib/id-generator')
|
const idGenerator = require('mantra-db-models/src/lib/id-generator');
|
||||||
|
const Token = require('markdown-it/lib/token');
|
||||||
|
const md = require("markdown-it")().disable(['link'])
|
||||||
|
|
||||||
module.exports = function (options) {
|
module.exports = function (options) {
|
||||||
const db = options.db;
|
const db = options.db;
|
||||||
var router = express.Router();
|
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')
|
router.route('/:id')
|
||||||
.get(function(request, response, next) {
|
.get(function(request, response, next) {
|
||||||
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
db.TranslationArtifactVersion.findByPk(request.params.id, {
|
||||||
@@ -186,6 +216,10 @@ module.exports = function (options) {
|
|||||||
.get(function(request, response, next) {
|
.get(function(request, response, next) {
|
||||||
db.TranslationChapter.findByPk(request.params.chapterId, {
|
db.TranslationChapter.findByPk(request.params.chapterId, {
|
||||||
include: [
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.Chapter,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
association: db.TranslationChapter.TranslationChunks,
|
association: db.TranslationChapter.TranslationChunks,
|
||||||
include: [
|
include: [
|
||||||
@@ -215,9 +249,16 @@ module.exports = function (options) {
|
|||||||
]
|
]
|
||||||
}).then(translationChapter => {
|
}).then(translationChapter => {
|
||||||
if (translationChapter) {
|
if (translationChapter) {
|
||||||
|
const tokens = produceTranslatedChapterOutput(translationChapter)
|
||||||
|
// TODO: Make markdown string...
|
||||||
|
|
||||||
response.display("translate-chapter-view", {
|
response.display("translate-chapter-view", {
|
||||||
user: request.user,
|
user: request.user,
|
||||||
pageTitle: `Translate Chapter ${translationChapter.name}`,
|
pageTitle: `Translate Chapter ${translationChapter.name}`,
|
||||||
|
translatedChapterOutput: md.renderer.render(
|
||||||
|
tokens,
|
||||||
|
{}
|
||||||
|
),
|
||||||
translationChapter: translationChapter
|
translationChapter: translationChapter
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -228,6 +269,141 @@ module.exports = function (options) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.route('/:id/chapter/:chapterId/html')
|
||||||
|
.get(function(request, response, next) {
|
||||||
|
db.TranslationChapter.findByPk(request.params.chapterId, {
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.Chapter,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.TranslationChunks,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationChunk.Translation
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.ArtifactVersion.Artifact,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.Artifact.Dialect
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}).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)
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
next(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
router.route('/:id/chapter/:chapterId/markdown')
|
||||||
|
.get(function(request, response, next) {
|
||||||
|
db.TranslationChapter.findByPk(request.params.chapterId, {
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.Chapter,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.TranslationChunks,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationChunk.Translation
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
association: db.TranslationChapter.TranslationArtifactVersion,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.TranslationArtifactVersion.ArtifactVersion,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.ArtifactVersion.Artifact,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
association: db.Artifact.Dialect
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}).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" || token.type == "bullet_list_open") {
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.type == "bullet_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 + "- "
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.type == "inline") {
|
||||||
|
return accumulator + token.content
|
||||||
|
}
|
||||||
|
return accumulator + token.markup
|
||||||
|
},
|
||||||
|
""
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
next(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
|
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
|
||||||
.get(function(request, response, next) {
|
.get(function(request, response, next) {
|
||||||
const previousIndex = Number(request.params.chunkIndex)-1
|
const previousIndex = Number(request.params.chunkIndex)-1
|
||||||
|
|||||||
@@ -9,14 +9,10 @@ block content
|
|||||||
//- TODO: export to markdown...
|
//- TODO: export to markdown...
|
||||||
.row
|
.row
|
||||||
.col.s12
|
.col.s12
|
||||||
a.btn.black(href=`/translate/${translationChapter.translationArtifactVersion.id}/chapter/${translationChapter.id}/markdown`) export markdown
|
a.btn.black(href=`/translate/${translationChapter.translationArtifactVersion.id}/chapter/${translationChapter.id}/markdown`, target="_blank") export markdown
|
||||||
|
|
||||||
.row
|
.row
|
||||||
.col.s12
|
.col.s12
|
||||||
each translationChunk in translationChapter.translationChunks.sort((a,b) => a.index - b.index)
|
|
||||||
p.flow-text
|
div !{translatedChapterOutput}
|
||||||
if translationChunk.translation
|
|
||||||
span= translationChunk.translation.text
|
|
||||||
else
|
|
||||||
span.grey-text= translationChunk.text
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user