[sources][nouns] allow linking from dictionary entries to specific fragment of example

This commit is contained in:
Andrea Vos 2021-05-19 20:29:34 +02:00
parent d6129b95df
commit 58fda3d400
2 changed files with 23 additions and 3 deletions

View File

@ -26,7 +26,7 @@ const approve = async (db, id) => {
const addVersions = async (req, nouns) => {
const keys = new Set();
nouns.filter(s => !!s && s.sources)
.forEach(s => s.sources.split(',').forEach(k => keys.add(`'` + k + `'`)));
.forEach(s => s.sources.split(',').forEach(k => keys.add(`'` + k.split('#')[0] + `'`)));
const sources = await req.db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
@ -41,11 +41,31 @@ const addVersions = async (req, nouns) => {
sources.forEach(s => sourcesMap[s.key] = s)
return nouns.map(n => {
n.sourcesData = (n.sources ? n.sources.split(',') : []).map(s => sourcesMap[s]);
n.sourcesData = (n.sources ? n.sources.split(',') : []).map(s => selectFragment(sourcesMap, s));
return n;
});
};
const selectFragment = (sourcesMap, keyAndFragment) => {
const [key, fragment] = keyAndFragment.split('#');
if (sourcesMap[key] === undefined) {
return undefined;
}
if (fragment === undefined) {
return sourcesMap[key];
}
const source = {...sourcesMap[key]};
const fragments = source.fragments
? source.fragments.replace('\\@', '###').split('@').map(x => x.replace('###', '\\@'))
: [];
source.fragments = fragments[parseInt(fragment) - 1];
return source;
}
const router = Router();
router.get('/nouns', async (req, res) => {

View File

@ -114,7 +114,7 @@ export class Source {
this.extra = extra;
this.year = year;
this.fragments = fragments
? fragments.replace(/\|/g, '\n').replace('\\@', '###').split('@').map(x => x.replace('###', '\@'))
? fragments.replace(/\|/g, '\n').replace('\\@', '###').split('@').map(x => x.replace('###', '\\@'))
: [];
this.comment = comment;
this.link = link;