All files / scripts/lib/normalize normalize.js

100% Statements 17/17
100% Branches 21/21
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                      13x 11x 9x 9x                     7x 5x 5x 2x   5x 3x   5x 2x     5x     1x  
#!/usr/bin/env node
 
/**
 * Converts a relative or same-origin docs path to an absolute raw.githubusercontent.com URL.
 * Absolute URLs are returned unchanged.
 *
 * @param {string} href - Link extracted from a README
 * @param {string} repoName - GitHub repository name (used to build the raw URL)
 * @returns {string} Absolute URL
 */
function toRawGithub(href, repoName) {
  if (!href) return href;
  if (/^https?:\/\//i.test(href)) return href;
  const p = String(href).trim().replace(/^\.\//, '').replace(/^\//, '');
  return `https://raw.githubusercontent.com/keglev/${repoName}/main/${p}`;
}
 
/**
 * Ensures all link fields inside a node's repoDocs object are absolute URLs.
 * Relative paths (e.g. `docs/api.html`) are expanded via toRawGithub.
 *
 * @param {object} node - Repository node with optional `repoDocs` sub-object
 * @returns {object} The same node, mutated in place
 */
function normalizeRepoDocsLinks(node) {
  if (!node || !node.repoDocs) return node;
  try {
    if (node.repoDocs.architectureOverview && node.repoDocs.architectureOverview.link) {
      node.repoDocs.architectureOverview.link = toRawGithub(node.repoDocs.architectureOverview.link, node.name);
    }
    if (node.repoDocs.apiDocumentation && node.repoDocs.apiDocumentation.link) {
      node.repoDocs.apiDocumentation.link = toRawGithub(node.repoDocs.apiDocumentation.link, node.name);
    }
    if (node.repoDocs.testing && node.repoDocs.testing.testingDocs && node.repoDocs.testing.testingDocs.link) {
      node.repoDocs.testing.testingDocs.link = toRawGithub(node.repoDocs.testing.testingDocs.link, node.name);
    }
  } catch (e) { /* swallow */ }
  return node;
}
 
module.exports = { toRawGithub, normalizeRepoDocsLinks };