File size: 5,695 Bytes
99c7c9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 * Pulpie WebUI - HTML Simplifier and Block Extractor
 *
 * Client-side DOM parsing and segmentation. Cleans extraneous elements,
 * resolves relative URLs to absolute URLs, tags semantic blocks with _item_id,
 * and extracts individual block HTML strings for EuroBERT classification.
 */

export const ITEM_ID_ATTR = '_item_id';

const REMOVAL_SELECTORS = [
  'script',
  'style',
  'noscript',
  'iframe',
  'frame',
  'object',
  'embed',
  'canvas',
  'svg',
  'link[rel="stylesheet"]',
  'meta',
  '[hidden]',
  '[aria-hidden="true"]',
].join(',');

const BLOCK_TAGS = new Set([
  'P',
  'H1',
  'H2',
  'H3',
  'H4',
  'H5',
  'H6',
  'BLOCKQUOTE',
  'PRE',
  'TABLE',
  'UL',
  'OL',
  'LI',
  'FIGURE',
  'ARTICLE',
  'SECTION',
  'HEADER',
  'FOOTER',
  'ASIDE',
  'NAV',
  'DIV',
]);

/**
 * Check if element has CSS rules hiding it completely.
 * @param {Element} element
 * @returns {boolean}
 */
function isElementInvisible(element) {
  const style = element.getAttribute('style') || '';
  return /display\s*:\s*none|visibility\s*:\s*hidden|opacity\s*:\s*0/i.test(style);
}

/**
 * Convert relative URLs in <img> and <a> tags to absolute URLs using baseUrl.
 * @param {Document} doc
 * @param {string} baseUrl
 */
export function resolveRelativeUrls(doc, baseUrl) {
  if (!baseUrl) return;
  try {
    const base = new URL(baseUrl);

    // Images
    const images = doc.querySelectorAll('img');
    for (const img of images) {
      const src = img.getAttribute('src');
      if (src && !src.startsWith('data:') && !src.startsWith('blob:') && !src.startsWith('javascript:')) {
        try {
          img.setAttribute('src', new URL(src, base).href);
        } catch {
          // ignore malformed URLs
        }
      }

      const dataSrc = img.getAttribute('data-src');
      if (dataSrc && !dataSrc.startsWith('data:') && !dataSrc.startsWith('blob:')) {
        try {
          const absDataSrc = new URL(dataSrc, base).href;
          img.setAttribute('data-src', absDataSrc);
          if (!img.getAttribute('src')) img.setAttribute('src', absDataSrc);
        } catch {
          // ignore
        }
      }
    }

    // Links
    const links = doc.querySelectorAll('a[href]');
    for (const a of links) {
      const href = a.getAttribute('href');
      if (href && !href.startsWith('#') && !href.startsWith('javascript:') && !href.startsWith('mailto:')) {
        try {
          a.setAttribute('href', new URL(href, base).href);
        } catch {
          // ignore
        }
      }
    }
  } catch (err) {
    console.warn('[HTMLSimplifier] Failed to parse base URL:', baseUrl, err);
  }
}

/**
 * Simplify and segment HTML into identifiable blocks.
 *
 * @param {string} rawHtml - Source HTML string
 * @param {string} [baseUrl=''] - Optional base URL for resolving relative links/images
 * @returns {{
 *   doc: Document,
 *   blocks: string[],
 *   itemIds: string[],
 *   title: string,
 *   totalNodes: number
 * }}
 */
export function simplifyAndExtractBlocks(rawHtml, baseUrl = '') {
  const parser = new DOMParser();
  const doc = parser.parseFromString(rawHtml || '', 'text/html');

  // Extract page title
  const titleElem = doc.querySelector('title');
  const title = titleElem ? titleElem.textContent.trim() : '';

  // Resolve relative URLs if baseUrl supplied
  if (baseUrl) {
    resolveRelativeUrls(doc, baseUrl);
  }

  // Remove non-content elements
  const removeTargets = doc.querySelectorAll(REMOVAL_SELECTORS);
  for (const el of removeTargets) {
    el.remove();
  }

  // Remove invisible elements
  const allElements = doc.body ? doc.body.querySelectorAll('*') : [];
  for (const el of allElements) {
    if (isElementInvisible(el)) {
      el.remove();
    }
  }

  const blocks = [];
  const itemIds = [];
  let currentId = 0;

  /**
   * Traverse nodes to identify semantic content blocks.
   * @param {Node} node
   */
  function collectBlocks(node) {
    if (!node) return;

    if (node.nodeType === Node.ELEMENT_NODE) {
      const tag = node.tagName.toUpperCase();

      // Check if this element is a standalone content block
      const isLeafContent =
        ['P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'PRE', 'TABLE', 'LI', 'FIGURE'].includes(tag);

      // Or an <img> tag with src
      const isImage = tag === 'IMG' && Boolean(node.getAttribute('src'));

      if (isLeafContent || isImage) {
        const textContent = node.textContent?.trim() || '';
        const hasImg = node.querySelector('img') !== null;

        // Skip empty whitespace blocks unless they contain images or tables
        if (textContent.length > 0 || hasImg || tag === 'TABLE' || isImage) {
          const itemId = String(currentId++);
          node.setAttribute(ITEM_ID_ATTR, itemId);
          blocks.push(node.outerHTML);
          itemIds.push(itemId);
          return;
        }
      }

      // If it's a container element (DIV, SECTION, ARTICLE, MAIN, BODY)
      for (const child of Array.from(node.childNodes)) {
        collectBlocks(child);
      }
    } else if (node.nodeType === Node.TEXT_NODE) {
      const text = node.textContent?.trim() || '';
      if (text.length > 10 && node.parentNode && !node.parentNode.hasAttribute(ITEM_ID_ATTR)) {
        // Wrap naked text runs
        const p = doc.createElement('p');
        p.textContent = text;
        const itemId = String(currentId++);
        p.setAttribute(ITEM_ID_ATTR, itemId);
        node.parentNode.replaceChild(p, node);
        blocks.push(p.outerHTML);
        itemIds.push(itemId);
      }
    }
  }

  if (doc.body) {
    collectBlocks(doc.body);
  }

  return {
    doc,
    blocks,
    itemIds,
    title,
    totalNodes: currentId,
  };
}