Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 933: Add translators to narrative parsing #937

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 60 additions & 8 deletions cli/server/parseNarrative.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ const makeFrontMatterBlock = (frontMatter) => {
const markdown = [];
markdown.push(`# ${frontMatter.title}`);
if (frontMatter.authors) {
if (typeof frontMatter.authors === 'object' && Array.isArray(frontMatter.authors)) {
utils.warn(`Narrative parsing -- can't do author arrays yet`);
} else if (typeof frontMatter.authors === 'string') {
if (frontMatter.authorLinks && typeof frontMatter.authorLinks === "string") {
markdown.push(`### Author: [${frontMatter.authors}](${frontMatter.authorLinks})`);
} else {
markdown.push(`### Author: ${frontMatter.authors}`);
}
let authors = parseContributors(frontMatter, "authors", "authorLinks");
if (authors) {
markdown.push(`### Author: ${authors}`);
if (frontMatter.affiliations && typeof frontMatter.affiliations === "string") {
markdown[markdown.length-1] += " <sup> 1 </sup>";
markdown.push(`<sup> 1 </sup> ${frontMatter.affiliations}`);
}
}
}
if (frontMatter.translators) {
let translators = parseContributors(frontMatter, "translators", "translatorLinks");
if (translators) {
markdown.push(`### Translators: ${translators}`);
}
}
if (frontMatter.date && typeof frontMatter.date === "string") {
markdown.push(`### Created: ${frontMatter.date}`);
}
Expand All @@ -58,6 +59,57 @@ const makeFrontMatterBlock = (frontMatter) => {
return block;
};

const parseContributors = (frontMatter, contributorsKey, contributorLinksKey) => {
const contributors = frontMatter[contributorsKey];
const contributorLinks = frontMatter[contributorLinksKey];

if (Array.isArray(contributors)) {
return parseContributorsArray(contributors, contributorLinks, contributorsKey, contributorLinksKey);
} else if (typeof contributors === 'string') {
return parseContributorsString(contributors, contributorLinks, contributorsKey, contributorLinksKey);
}
}

const parseContributorsArray = (contributors, contributorLinks, contributorsKey, contributorLinksKey) => {
// validate links
if (contributorLinks) {
if (!Array.isArray(contributorLinks)) {
utils.warn(`Narrative parsing - if ${contributorsKey} is an array, then ${contributorLinksKey} must also be an array. Skipping links.`);
contributorLinks = undefined;
} else if (contributorLinks.length != contributors.length) {
utils.warn(`Narrative parsing - the length of ${contributorsKey} and ${contributorLinksKey} did not match. Skipping links.`);
contributorLinks = undefined;
}
}

if (contributorLinks) {
contributors = contributors.map((contributor, idx) => {
return contributorLink(contributor, contributorLinks[idx]);
});
}

return contributors.join(", ");
}

const parseContributorsString = (contributors, contributorLinks, contributorsKey, contributorLinksKey) => {
// validate links
if (contributorLinks) {
if (typeof contributorLinks !== "string") {
utils.warn(`Narrative parsing - if ${contributorsKey} is a string, then ${contributorLinksKey} must also be a string. Skipping links.`);
contributorLinks = undefined;
}
}

return contributorLink(contributors, contributorLinks);
}

const contributorLink = (contributor, contributorLink) => {
if (contributorLink) {
return `[${contributor}](${contributorLink})`;
}
return contributor;
}

/**
* Extract a code-block from the content, if it exists, tagged as auspiceMainDisplayMarkdown
* Which we send to the client under a special key for parsing in the main display
Expand Down