| 123 | } |
| 124 | |
| 125 | function extractRecords(url, html) { |
| 126 | const $ = load(html); |
| 127 | const contentRoot = $("#markdown-content"); |
| 128 | |
| 129 | if (!contentRoot.length) { |
| 130 | return []; |
| 131 | } |
| 132 | |
| 133 | const title = |
| 134 | textOf($, ".vp-page-title h1") || |
| 135 | textOf($, "main h1") || |
| 136 | textOf($, "title") || |
| 137 | "JavaGuide"; |
| 138 | |
| 139 | const hierarchy = { |
| 140 | lvl0: "JavaGuide", |
| 141 | lvl1: title, |
| 142 | lvl2: null, |
| 143 | lvl3: null, |
| 144 | lvl4: null, |
| 145 | lvl5: null, |
| 146 | lvl6: null, |
| 147 | }; |
| 148 | const records = [ |
| 149 | recordFor({ |
| 150 | url, |
| 151 | title, |
| 152 | hierarchy: { ...hierarchy }, |
| 153 | content: null, |
| 154 | anchor: null, |
| 155 | type: "lvl1", |
| 156 | position: 0, |
| 157 | }), |
| 158 | ]; |
| 159 | let currentAnchor = null; |
| 160 | |
| 161 | contentRoot |
| 162 | .find("h2,h3,h4,h5,h6,p,li,td,blockquote") |
| 163 | .each((index, element) => { |
| 164 | const tag = element.name; |
| 165 | const content = $(element).text().replace(/\s+/g, " ").trim(); |
| 166 | |
| 167 | if (!content) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | const isHeading = /^h[2-6]$/.test(tag); |
| 172 | |
| 173 | if (isHeading) { |
| 174 | const level = Number(tag.slice(1)); |
| 175 | |
| 176 | for (let i = level; i <= 6; i += 1) { |
| 177 | hierarchy[`lvl${i}`] = null; |
| 178 | } |
| 179 | |
| 180 | hierarchy[`lvl${level}`] = content; |
| 181 | currentAnchor = $(element).attr("id") || currentAnchor; |
| 182 | } |