@canalplus/readme.doc
Version:
Readme's an Extremely Accessible Documentation MakEr
83 lines (82 loc) • 3.21 kB
JavaScript
/**
* Specialized class here to store then check availability of documentation
* "anchors" referenced in the various pages.
* @class AnchorChecker
*/
export default class AnchorChecker {
constructor() {
this._anchorsPerFile = new Map();
this._anchorsList = [];
}
/**
* Add a list of anchors set in a particular input file.
* @param {string} inputFile - The Markdown file from which the anchors are.
* @param {Array.<string>} anchors - Every anchors in that file.
*/
addAnchorsForFile(inputFile, anchors) {
this._anchorsPerFile.set(inputFile, anchors);
}
/**
* Adds a reference to an anchor seen in an input file.
*
* This method doesn't check the validity of that anchor yet, it just adds
* it to the `AnchorChecker`'s local queue of referenced anchors which can
* then be checked all at once by calling `checkAllAnchors`.
* @param {string} inputFileWithLink - The input file where the anchored link
* was found.
* @param {string} inputFileLinkDestination - The targeted input file where
* the anchor should be found.
* @param {string} anchor - The actual anchor.
*/
addAnchorReference(inputFileWithLink, inputFileLinkDestination, anchor) {
this._anchorsList.push({
inputFileWithLink,
inputFileLinkDestination,
anchor,
});
}
/**
* Check the validity of all anchors added through `addAnchorReference`
* method calls and return descriptions of checks which failed.
* @returns {Array.<Object>} - Description of every anchor check that
* failed, or an empty array if all anchors are valid.
*/
checkAllAnchors() {
const badResults = [];
for (const elt of this._anchorsList) {
const validity = this.checkValidAnchor(elt.inputFileLinkDestination, elt.anchor);
if (validity !== 0 /* AnchorValidity.Found */) {
badResults.push(Object.assign(Object.assign({}, elt), { validity }));
}
}
return badResults;
}
/**
* Check the validity of a single anchor according to what has been
* communicated previously to the `AnchorChecker`.
* @param {string} file - The input file which that anchor applies to.
* @param {string} anchor - The actual anchor
* @returns {number} - Number describing that anchor's validity.
*/
checkValidAnchor(file, anchor) {
const anchorsForFile = this._anchorsPerFile.get(file);
if (anchorsForFile === undefined) {
return 1 /* AnchorValidity.FileNotFound */;
}
if (!anchorsForFile.includes(anchor)) {
return 2 /* AnchorValidity.AnchorNotFound */;
}
return 0 /* AnchorValidity.Found */;
}
/**
* Returns all anchors available in an input file according to what has been
* communicated previously to the `AnchorChecker`.
*
* Returns `undefined` if the input file was never added to the
* `AnchorChecker`.
* @returns {Array.<string>|undefined}
*/
getAnchorsForInputFile(inputFile) {
return this._anchorsPerFile.get(inputFile);
}
}