Skip to content

Commit

Permalink
refactor(core/biblio): add biblio to conf.state instead of export
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Feb 19, 2021
1 parent fb71510 commit 430cdce
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
31 changes: 12 additions & 19 deletions src/core/biblio.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@
import { biblioDB } from "./biblio-db.js";
import { createResourceHint } from "./utils.js";

/** @type {Conf['biblio']} */
export const biblio = {};

// for backward compatibity
export { wireReference, stringifyReference } from "./render-biblio.js";

export const name = "core/biblio";

const bibrefsURL = new URL("https://specref.herokuapp.com/bibrefs?refs=");

export async function prepare() {
export async function prepare(conf) {
// Opportunistically dns-prefetch to bibref server, as we don't know yet
// if we will actually need to download references yet.
const link = createResourceHint({
hint: "dns-prefetch",
href: bibrefsURL.origin,
});
document.head.appendChild(link);
}

let doneResolver;

/** @type {Promise<Conf['biblio']>} */
const done = new Promise(resolve => {
doneResolver = resolve;
});
conf.state[name] = {
/** @type {Conf['biblio']} */
biblio: {},
};
}

export async function updateFromNetwork(
refs,
Expand Down Expand Up @@ -65,17 +60,17 @@ export async function updateFromNetwork(
}

/**
* @param {Conf['biblio']} biblio
* @param {string} key
* @returns {Promise<BiblioData>}
* @returns {BiblioData}
*/
export async function resolveRef(key) {
const biblio = await done;
export function resolveRef(biblio, key) {
if (!biblio.hasOwnProperty(key)) {
return null;
}
const entry = biblio[key];
if (entry.aliasOf) {
return await resolveRef(entry.aliasOf);
return resolveRef(biblio, entry.aliasOf);
}
return entry;
}
Expand Down Expand Up @@ -133,12 +128,11 @@ export class Plugin {
}

async run() {
const finish = () => {
doneResolver(this.conf.biblio);
};
if (!this.conf.localBiblio) {
this.conf.localBiblio = {};
}
/** @type {Conf["biblio"]} */
const biblio = this.conf.state[name].biblio;
this.conf.biblio = biblio;
const localAliases = Object.keys(this.conf.localBiblio)
.filter(key => this.conf.localBiblio[key].hasOwnProperty("aliasOf"))
Expand Down Expand Up @@ -172,6 +166,5 @@ export class Plugin {
Object.assign(biblio, data);
}
Object.assign(biblio, this.conf.localBiblio);
finish();
}
}
23 changes: 13 additions & 10 deletions src/core/data-cite.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* `data-cite` to `href` attributes. `data-cite` attributes are added to markup
* directly by the author as well as via other modules like core/xref.
*/
import { biblio, resolveRef, updateFromNetwork } from "./biblio.js";
import {
refTypeFromContext,
showError,
showWarning,
wrapInner,
} from "./utils.js";
import { resolveRef, updateFromNetwork } from "./biblio.js";
import { sub } from "./pubsubhub.js";
export const name = "core/data-cite";

Expand All @@ -26,9 +26,10 @@ export const name = "core/data-cite";
export const THIS_SPEC = "__SPEC__";

/**
* @param {Conf["biblio"]} biblio
* @param {CiteDetails} citeDetails
*/
async function getLinkProps(citeDetails) {
function getLinkProps(biblio, citeDetails) {
const { key, frag, path } = citeDetails;
let href = "";
let title = "";
Expand All @@ -37,7 +38,7 @@ async function getLinkProps(citeDetails) {
href = document.location.href;
} else {
// Let's go look it up in spec ref...
const entry = await resolveRef(key);
const entry = resolveRef(biblio, key);
if (!entry) {
return null;
}
Expand Down Expand Up @@ -156,18 +157,20 @@ export function toCiteDetails(elem) {
return details;
}

export async function run() {
export async function run(conf) {
/** @type {Conf["biblio"]} */
const biblio = conf.state["core/biblio"].biblio;
/** @type {NodeListOf<HTMLElement>} */
const elems = document.querySelectorAll(
"dfn[data-cite]:not([data-cite='']), a[data-cite]:not([data-cite=''])"
);

await updateBiblio([...elems]);
await updateBiblio(biblio, [...elems]);

for (const elem of elems) {
const originalKey = elem.dataset.cite;
const citeDetails = toCiteDetails(elem);
const linkProps = await getLinkProps(citeDetails);
const linkProps = getLinkProps(biblio, citeDetails);
if (linkProps) {
linkElem(elem, linkProps, citeDetails);
} else {
Expand All @@ -181,14 +184,14 @@ export async function run() {

/**
* Fetch and update `biblio` with entries corresponding to given elements
* @param {Conf["biblio"]} biblio
* @param {HTMLElement[]} elems
*/
async function updateBiblio(elems) {
const promisesForBibEntries = elems.map(toCiteDetails).map(async entry => {
const result = await resolveRef(entry.key);
async function updateBiblio(biblio, elems) {
const bibEntries = elems.map(toCiteDetails).map(entry => {
const result = resolveRef(biblio, entry.key);
return { entry, result };
});
const bibEntries = await Promise.all(promisesForBibEntries);

const missingBibEntries = bibEntries
.filter(({ result }) => result === null)
Expand Down
17 changes: 11 additions & 6 deletions src/core/render-biblio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// renders the biblio data pre-processed in core/biblio

import { addId, getIntlData, showError } from "./utils.js";
import { biblio } from "./biblio.js";
import { html } from "./import-maps.js";

export const name = "core/render-biblio";
Expand Down Expand Up @@ -73,6 +72,8 @@ const endWithDot = endNormalizer(".");

/** @param {Conf} conf */
export function run(conf) {
/** @type {Conf["biblio"]} */
const biblio = conf.state["core/biblio"].biblio;
const informs = Array.from(conf.informativeReferences);
const norms = Array.from(conf.normativeReferences);

Expand All @@ -90,24 +91,27 @@ export function run(conf) {
refSection.classList.add("appendix");

if (norms.length) {
const sec = createReferencesSection(norms, l10n.norm_references);
const sec = createReferencesSection(biblio, norms, l10n.norm_references);
refSection.appendChild(sec);
}
if (informs.length) {
const sec = createReferencesSection(informs, l10n.info_references);
const sec = createReferencesSection(biblio, informs, l10n.info_references);
refSection.appendChild(sec);
}

document.body.appendChild(refSection);
}

/**
* @param {Conf["biblio"]} biblio
* @param {string[]} refs
* @param {string} title
* @returns {HTMLElement}
*/
function createReferencesSection(refs, title) {
const { goodRefs, badRefs } = groupRefs(refs.map(toRefContent));
function createReferencesSection(biblio, refs, title) {
const { goodRefs, badRefs } = groupRefs(
refs.map(ref => toRefContent(biblio, ref))
);
const uniqueRefs = getUniqueRefs(goodRefs);

const refsToShow = uniqueRefs
Expand All @@ -132,10 +136,11 @@ function createReferencesSection(refs, title) {
/**
* returns refcontent and unique key for a reference among its aliases
* and warns about circular references
* @param {Conf["biblio"]} biblio
* @param {String} ref
* @typedef {ReturnType<typeof toRefContent>} Ref
*/
function toRefContent(ref) {
function toRefContent(biblio, ref) {
let refcontent = biblio[ref];
let key = ref;
const circular = new Set([key]);
Expand Down
1 change: 1 addition & 0 deletions src/type-helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ interface Conf {
localBiblio?: Record<string, BiblioData>;
biblio: Record<string, BiblioData>;
shortName: string;
state: Record<string, Record<string, any>>;
}

type ResourceHintOption = {
Expand Down
7 changes: 4 additions & 3 deletions src/w3c/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export async function run(conf) {
}

async function addJSONLDInfo(conf, doc) {
/** @type {Conf["biblio"]} */
const biblio = conf.state["core/biblio"].biblio;

// Content for JSON
const type = ["TechArticle"];
if (conf.rdfStatus) type.push(conf.rdfStatus);
Expand Down Expand Up @@ -133,9 +136,7 @@ async function addJSONLDInfo(conf, doc) {
...conf.normativeReferences,
...conf.informativeReferences,
];
const citationContents = await Promise.all(
citationIds.map(ref => resolveRef(ref))
);
const citationContents = citationIds.map(ref => resolveRef(biblio, ref));
jsonld.citation = citationContents
.filter(ref => typeof ref === "object")
.map(addRef);
Expand Down

0 comments on commit 430cdce

Please sign in to comment.