mirror of
https://github.com/wassname/template.git
synced 2026-07-20 12:40:52 +08:00
Add support for external bibtex files
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user