Add word+character count
This commit is contained in:
@@ -1,9 +1,48 @@
|
||||
const db = require('mantra-db-models');
|
||||
const md = require("markdown-it")().disable(['link'])
|
||||
const express = require('express');
|
||||
|
||||
const wordCounter = (totalWordCount, chunk) => {
|
||||
return totalWordCount + chunk.split(" ").length
|
||||
}
|
||||
|
||||
const characterCounter = (totalCharacterCount, chunk) => {
|
||||
return totalCharacterCount + chunk.length
|
||||
}
|
||||
|
||||
const saveChapter = async (userId, chapterName, text, chapterIndex, artifactVersion) => {
|
||||
const chunks = md.parse(
|
||||
text
|
||||
).filter(token => token.content).map(token => token.content/*.replace(/\n/g,' ')*/)
|
||||
return db.Chapter.create({
|
||||
creatorId: userId,
|
||||
name: chapterName,
|
||||
originalText: text,
|
||||
wordCount: chunks.reduce(wordCounter, 0),// TODO: Count words...
|
||||
characterCount: chunks.reduce(characterCounter, 0),// TODO: Count words...
|
||||
index: chapterIndex,
|
||||
artifactVersionId: artifactVersion.id,
|
||||
chunks: chunks.map((chunk,index) => {
|
||||
return {
|
||||
creatorId: userId,
|
||||
text: chunk.trim(),
|
||||
index: index,
|
||||
wordCount: chunk.split(" ").length,
|
||||
characterCount: chunk.length
|
||||
}
|
||||
})
|
||||
}, {
|
||||
include: [
|
||||
{
|
||||
association: db.Chapter.Chunks
|
||||
}
|
||||
]
|
||||
}).catch(error => {
|
||||
console.error("Chapter Upload Error: ", error)
|
||||
return null
|
||||
})
|
||||
}
|
||||
module.exports = function (options) {
|
||||
const db = options.db;
|
||||
var router = express.Router();
|
||||
|
||||
router.route('/:id')
|
||||
@@ -219,6 +258,131 @@ module.exports = function (options) {
|
||||
})
|
||||
})
|
||||
|
||||
router.route('/:id/chapters/upload')
|
||||
.get(function(request, response, next) {
|
||||
db.ArtifactVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.ArtifactVersion.Artifact,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Artifact.ArtifactApproval,
|
||||
},
|
||||
{
|
||||
association: db.Artifact.Owner,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Owner.OwnerEntities,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.OwnerEntity.Entity,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Entity.EntityUsers,
|
||||
required: true,
|
||||
where: {
|
||||
userId: request.user?.id ?? null
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.ArtifactVersion.Chapters
|
||||
}
|
||||
]
|
||||
}).then(artifactVersion => {
|
||||
if (artifactVersion) {
|
||||
response.display("chapters-upload", {
|
||||
user: request.user,
|
||||
pageTitle: "Library - Mantra",
|
||||
artifactVersion: artifactVersion
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
.post(function(request, response, next) {
|
||||
db.ArtifactVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.ArtifactVersion.Artifact,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Artifact.ArtifactApproval,
|
||||
},
|
||||
{
|
||||
association: db.Artifact.Owner,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Owner.OwnerEntities,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.OwnerEntity.Entity,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Entity.EntityUsers,
|
||||
required: true,
|
||||
where: {
|
||||
userId: request.user?.id ?? null
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.ArtifactVersion.Chapters
|
||||
}
|
||||
]
|
||||
}).then(async (artifactVersion) => {
|
||||
if (artifactVersion && request.files) {
|
||||
// TODO: Parse files and store their texts...
|
||||
const chapterIndex = artifactVersion.chapters.length + 1
|
||||
for (const index in request.files.chapters) {
|
||||
const file = request.files.chapters[index]
|
||||
const fileText = file.data.toString()
|
||||
// if (Object.hasOwnProperty.call(file, index)) {
|
||||
// const file = request.files[index]
|
||||
// }
|
||||
const chapter = await saveChapter(
|
||||
request.user.id,
|
||||
file.name,
|
||||
fileText,
|
||||
chapterIndex+index,
|
||||
artifactVersion
|
||||
)
|
||||
}
|
||||
response.redirect(`/v/${artifactVersion.id}`)
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
|
||||
router.route('/:id/chapters/:chapterId')
|
||||
.get(function(request, response, next) {
|
||||
db.Chapter.findByPk(request.params.chapterId, {
|
||||
|
||||
Reference in New Issue
Block a user