Output markdown

This commit is contained in:
kngako
2023-01-09 00:45:42 +02:00
parent 2887197c4d
commit df9c01201f
2 changed files with 182 additions and 10 deletions

View File

@@ -1,11 +1,41 @@
const express = require('express')
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) {
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, {
@@ -186,6 +216,10 @@ module.exports = function (options) {
.get(function(request, response, next) {
db.TranslationChapter.findByPk(request.params.chapterId, {
include: [
{
association: db.TranslationChapter.Chapter,
required: true
},
{
association: db.TranslationChapter.TranslationChunks,
include: [
@@ -215,9 +249,16 @@ 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,
{}
),
translationChapter: translationChapter
})
} else {
@@ -227,6 +268,141 @@ module.exports = function (options) {
next(error)
})
})
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')
.get(function(request, response, next) {

View File

@@ -9,14 +9,10 @@ block content
//- TODO: export to markdown...
.row
.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
.col.s12
each translationChunk in translationChapter.translationChunks.sort((a,b) => a.index - b.index)
p.flow-text
if translationChunk.translation
span= translationChunk.translation.text
else
span.grey-text= translationChunk.text
.row
.col.s12
div !{translatedChapterOutput}