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, {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const config = require('config');
|
||||
const express = require('express');
|
||||
const fileUpload = require('express-fileupload');
|
||||
const pug = require("pug");
|
||||
const session = require("express-session");
|
||||
const SequelizeSessionStore = require('connect-session-sequelize')(session.Store);
|
||||
@@ -20,6 +21,8 @@ module.exports = function () {
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(fileUpload());
|
||||
|
||||
app.set("view engine", "pug");
|
||||
app.set("views", path.resolve("server/views"));
|
||||
|
||||
|
||||
@@ -41,7 +41,10 @@ block content
|
||||
if isOwnedByUser
|
||||
.divider
|
||||
.row
|
||||
a.btn.black(href=`/v/${artifactVersion.id}/chapters/add`) add chapter
|
||||
.col.s12
|
||||
a.btn.black(href=`/v/${artifactVersion.id}/chapters/add`) add chapter
|
||||
.col.s12
|
||||
a.btn.black(href=`/v/${artifactVersion.id}/chapters/upload`) upload chapter(s)
|
||||
|
||||
if artifactVersion.chapters.length == 0
|
||||
p.flow-text No chapters added
|
||||
|
||||
21
server/views/chapters-upload.pug
Normal file
21
server/views/chapters-upload.pug
Normal file
@@ -0,0 +1,21 @@
|
||||
extend templates/layout.pug
|
||||
|
||||
block content
|
||||
.container
|
||||
.center
|
||||
h1 Chapter in #{artifactVersion.name}
|
||||
|
||||
.row
|
||||
.col.s12
|
||||
form.row(action=`/v/${artifactVersion.id}/chapters/upload`, method="post", encType="multipart/form-data")
|
||||
//- .col.s12.input-field
|
||||
textarea#textarea.materialize-textarea(name="text")
|
||||
label(for="textarea") Entire chapter text (markdown)
|
||||
.file-field.input-field.col.s12
|
||||
.btn
|
||||
span File
|
||||
input#file-input(type="file", name="chapters" multiple)
|
||||
.file-path-wrapper
|
||||
input.file-path.validate(type="text", placeholder="Upload one or more file with the chapter content")
|
||||
.col.s12
|
||||
button.btn.black(type="submit") upload chapters
|
||||
Reference in New Issue
Block a user