diff --git a/src/distill.js b/src/distill.js index fc50075..001325c 100644 --- a/src/distill.js +++ b/src/distill.js @@ -347,6 +347,12 @@ export function distill(html, url = '', { view = null, type = '' } = {}) { // space that was never in the page. if (text) { blocks.push({ type: 'text', text, host: node.parentNode, pre: /^\s/.test(raw), post: /\s$/.test(raw) }); + } else if (raw && blocks.at(-1)?.type === 'text') { + // A node that is only a space still separates the words either side + // of it. Wikipedia writes every   as a span holding nothing + // else, and the fragments around it share a parent, so without this + // an infobox read "Firstappeared" and "20February 1991". + blocks.at(-1).post = true; } return; } diff --git a/tests/distill.test.js b/tests/distill.test.js index bc3b31c..ffd60e9 100644 --- a/tests/distill.test.js +++ b/tests/distill.test.js @@ -368,6 +368,20 @@ test('an entity does not put spaces inside a word', () => { assert.ok(text.includes('interface & its restraint'), 'a real space was swallowed'); }); +test('a space in an element of its own still separates two words', () => { + // Wikipedia's HTML writes every   as  . + // That span has no text once whitespace is collapsed, and the words either + // side of it were glued: "Firstappeared", "20February 1991". + const p = distill(` + + +
First appeared20 February 1991

ten items

`, 'https://example.test/'); + const text = p.blocks.map((b) => b.text).join('\n'); + assert.ok(text.includes('First appeared'), `words glued:\n${text}`); + assert.ok(text.includes('20 February 1991'), `words glued:\n${text}`); + assert.ok(text.includes('ten items'), `words glued:\n${text}`); +}); + test('a one-character value is kept, a lone bullet or separator is not', () => { // A Wikipedia infobox puts each value in a cell of its own, so a single // digit is a whole block: "Children" was printed with no number after it.