All files / src/components/RepoDocs useRepoDocs.js

100% Statements 30/30
92.68% Branches 38/41
100% Functions 11/11
100% Lines 22/22

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76      139x   1x 139x 23x                       23x                   2x           23x                             1x 14x   14x 8x 8x 8x 8x 7x 6x 6x 23x     1x     8x 8x     14x        
import { useState, useEffect } from 'react';
 
// Filters out generic GitHub CTA headings that bleed into title fields when READMEs are parsed
const isBadTitle = (s) => !s || /open an issue|question|contribut/i.test(String(s));
 
const safeTitle = (p) => {
  const valid = (s) => (isBadTitle(s) ? null : s);
  return (
    valid(p.repoDocs?.apiDocumentation?.title_de) ||
    valid(p.repoDocs?.architectureOverview?.title_de) ||
    valid(p.docsTitle_de) ||
    valid(p.repoDocs?.apiDocumentation?.title) ||
    valid(p.repoDocs?.architectureOverview?.title) ||
    valid(p.docs?.documentation?.title) ||
    valid(p.docsTitle) ||
    null
  );
};
 
const hasRepoDocs = (p) => !!(
  p.repoDocs?.placeholder ||
  (p.repoDocs?.apiDocumentation?.link) ||
  (p.repoDocs?.architectureOverview?.link) ||
  (p.repoDocs?.testing?.coverage?.length > 0) ||
  (p.docs?.apiDocumentation?.link) ||
  (p.docs?.documentation?.link) ||
  p.docsLink ||
  // _ast is the parsed README abstract syntax tree; scan its headings as a last resort
  (p._ast?.children && Array.isArray(p._ast.children) && p._ast.children.some(
    c => c?.type === 'heading' && c.children?.length &&
      /doc|api|architectur/i.test(String(c.children[0]?.value || ''))
  )) ||
  (p.text && /documentation|api documentation|architecture|api integration/i.test(p.text))
);
 
const enrichProject = (p) => ({
  name: p.name,
  docs: p.docs || null,
  repoDocs: p.repoDocs || null,
  docsLink: p.docsLink || null,
  docsTitle: safeTitle(p),
  hasDocs: hasRepoDocs(p),
});
 
/**
 * Fetches projects.json and returns only entries that have linked documentation.
 * Enriches each project with a normalized docs shape via enrichProject.
 *
 * @returns {object[]} Array of enriched project objects where hasDocs is true
 */
const useRepoDocs = () => {
  const [projectsWithDocs, setProjectsWithDocs] = useState([]);
 
  useEffect(() => {
    let cancelled = false;
    const load = async () => {
      try {
        const res = await fetch('/projects.json');
        if (!res.ok) return setProjectsWithDocs([]);
        const data = await res.json();
        Eif (!cancelled) {
          setProjectsWithDocs(data.map(enrichProject).filter(e => e.hasDocs));
        }
      } catch (e) {
        Eif (!cancelled) setProjectsWithDocs([]);
      }
    };
    load();
    return () => { cancelled = true; };
  }, []);
 
  return projectsWithDocs;
};
 
export default useRepoDocs;