/
doremi
/
tat-dvor
Обзор
Документация
Войти
/
doremi
/
tat-dvor
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/parse-html.mjs
46 строк
1 KB
doremi
Create project
01 авг 2026, 18:45
01 авг 2026, 18:45
f2ee3ce
Код
Авторство
О чём код?
import * as cheerio from 'cheerio'; import { FIELDS, matchHeader, hasRequired } from './columns.mjs'; export function parseSpecHtml(html) { const $ = cheerio.load(html); const title = $('h1').first().text().trim(); let best = null; $('table').each((_, table) => { const $table = $(table); const rows = $table.find('tr').toArray(); if (rows.length < 2) return; const headerCells = $(rows[0]).find('th,td').toArray(); const columnMap = {}; headerCells.forEach((cell, idx) => { const field = matchHeader($(cell).text()); if (field && !(field in columnMap)) columnMap[field] = idx; }); if (!hasRequired(columnMap)) return; if (best && rows.length <= best.rows.length) return; best = { rows, columnMap }; }); if (!best) return { status: 'header-not-found', title, rows: [] }; const { rows, columnMap } = best; const out = []; for (const tr of rows.slice(1)) { const cells = $(tr).find('td,th').toArray(); if (!cells.length) continue; const item = {}; for (const field of FIELDS) { const idx = columnMap[field]; if (idx === undefined || !cells[idx]) continue; const $cell = $(cells[idx]); item[field] = field === 'image' ? ($cell.find('img').attr('src') || '').trim() : $cell.text().replace(/\s+/g, ' ').trim(); } if (!item.name && !item.qty) continue; out.push(item); } return { status: 'ok', title, rows: out }; }