private htmlToSections()

in functions/src/content.ts [206:241]


  private htmlToSections(html: string): PageContent {
    const $ = cheerio.load(html);
    const sections: PageSection[] = [];

    let $headerChildren = $("div", "<div></div>");

    let $h1 = $("h1").first();
    $h1.nextUntil("h2").each((_: number, el: any) => {
      $headerChildren = $headerChildren.append(el);
    });

    const header: PageSection = {
      name: $h1.text(),
      content: $headerChildren.html()
    };

    $("h2").each((_: number, el: CheerioElement) => {
      let $sibchils = $("div", "<div></div>");

      $(el)
        .nextUntil("h2")
        .each((_: number, el: any) => {
          $sibchils = $sibchils.append(el);
        });

      sections.push({
        name: $(el).text(),
        content: $sibchils.html()
      });
    });

    return {
      header,
      sections
    };
  }