Add support for external bibtex files

This commit is contained in:
Ludwig Schubert
2017-09-01 16:11:27 -07:00
parent 31982778f8
commit c69f39e25a
9 changed files with 162 additions and 144 deletions
+29 -16
View File
@@ -1,26 +1,39 @@
import { renderBibliography, templateString } from '../components/d-bibliography';
import { parseBibtex } from '../helpers/bibtex';
import fs from 'fs';
export default function(dom, data) {
const bibliographyTag = dom.querySelector('d-bibliography');
if (!bibliographyTag) {
console.warn('No bibliography tag found!');
console.warn('No bibliography tag present!');
return;
}
const bibliographyEntries = new Map(data.citations.map( citationKey => {
const entry = data.bibliography.get(citationKey);
return [citationKey, entry];
}));
const prerenderedBibliography = dom.createElement('d-bibliography-prerendered');
const template = dom.createElement('template');
template.innerHTML = templateString;
const clone = dom.importNode(template.content, true);
prerenderedBibliography.innerHTML = template.content;
renderBibliography(prerenderedBibliography, bibliographyEntries, dom);
bibliographyTag.parentElement.insertBefore(bibliographyTag, prerenderedBibliography);
bibliographyTag.parentElement.removeChild(bibliographyTag);
const src = bibliographyTag.getAttribute('src');
if (src) {
const path = data.inputDirectory + '/' + src;
const text = fs.readFileSync(path, 'utf-8');
const bibliography = parseBibtex(text);
const scriptTag = dom.createElement('script');
scriptTag.type = 'text/json';
scriptTag.textContent = JSON.stringify([...bibliography]);
bibliographyTag.appendChild(scriptTag);
bibliographyTag.removeAttribute('src');
}
// const bibliographyEntries = new Map(data.citations.map( citationKey => {
// const entry = data.bibliography.get(citationKey);
// return [citationKey, entry];
// }));
//
// const prerenderedBibliography = dom.createElement('d-bibliography-prerendered');
//
// const template = dom.createElement('template');
// template.innerHTML = templateString;
// const clone = dom.importNode(template.content, true);
// prerenderedBibliography.innerHTML = template.content;
// renderBibliography(prerenderedBibliography, bibliographyEntries, dom);
//
// bibliographyTag.parentElement.insertBefore(bibliographyTag, prerenderedBibliography);
// bibliographyTag.parentElement.removeChild(bibliographyTag);
}
+1 -7
View File
@@ -42,11 +42,5 @@ export default function render(dom) {
polyfillScriptTag.id = 'polyfills';
// insert at appropriate position--before any other script tag
const head = dom.querySelector('head');
const firstScriptTag = dom.querySelector('script');
if (firstScriptTag) {
head.insertBefore(polyfillScriptTag, firstScriptTag);
} else {
head.appendChild(polyfillScriptTag);
}
dom.head.insertBefore(polyfillScriptTag, dom.head.firstChild);
}
+5 -3
View File
@@ -3,7 +3,9 @@ import { renderTOC } from '../components/d-toc';
export default function(dom) {
const article = dom.querySelector('d-article');
const toc = dom.querySelector('d-toc');
const headings = article.querySelectorAll('h2, h3');
renderTOC(toc, headings);
toc.setAttribute('prerendered', 'true');
if (toc) {
const headings = article.querySelectorAll('h2, h3');
renderTOC(toc, headings);
toc.setAttribute('prerendered', 'true');
}
}