From 67ced7885e2d967da05ba792c9434645d124f032 Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Thu, 3 Oct 2024 09:38:06 +0200 Subject: [PATCH] fix: ignore paths as descriptions fixes #705 --- src/steps/extract-metadata.js | 2 +- .../content/description-gridtable.html | 18 ++++++++++++++++++ test/fixtures/content/description-gridtable.md | 16 ++++++++++++++++ test/rendering.test.js | 5 +++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/content/description-gridtable.html create mode 100644 test/fixtures/content/description-gridtable.md diff --git a/src/steps/extract-metadata.js b/src/steps/extract-metadata.js index 5be526d7..a98dc35d 100644 --- a/src/steps/extract-metadata.js +++ b/src/steps/extract-metadata.js @@ -137,7 +137,7 @@ function extractDescription(hast) { visit(hast, (node, idx, parent) => { if (parent?.tagName === 'div' && node.tagName === 'p') { const words = toString(node).trim().split(/\s+/); - if (words.length >= 10 || words.some((w) => w.length > 25 && !w.startsWith('http'))) { + if (words.length >= 10 || words.some((w) => w.length > 25 && !w.startsWith('http') && !w.startsWith('/'))) { desc = `${words.slice(0, 25).join(' ')}${words.length > 25 ? ' ...' : ''}`; return EXIT; } diff --git a/test/fixtures/content/description-gridtable.html b/test/fixtures/content/description-gridtable.html new file mode 100644 index 00000000..8e1bc898 --- /dev/null +++ b/test/fixtures/content/description-gridtable.html @@ -0,0 +1,18 @@ + + Title. + + + + + + + + + + + + + + + + diff --git a/test/fixtures/content/description-gridtable.md b/test/fixtures/content/description-gridtable.md new file mode 100644 index 00000000..01a4581c --- /dev/null +++ b/test/fixtures/content/description-gridtable.md @@ -0,0 +1,16 @@ +## Title. + +Short sentence < 10 words. + ++--------------------------------------------------------------------------------------+ +| Stock Locator Model Overview | ++--------------------------------------------------------------------------------------+ +| ## Details | +| | +| [/longlink/thathas/morethan/25/characters](/longlink/thathas/morethan/25/characters) | +| | ++--------------------------------------------------------------------------------------+ +| More Text | +| | +| And here the real description from a text that has more than 10 words.. | ++--------------------------------------------------------------------------------------+ diff --git a/test/rendering.test.js b/test/rendering.test.js index ccf8dcbc..5df78330 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -395,6 +395,11 @@ describe('Rendering', () => { await testRender('description', 'head'); }); + it('uses correct description from a gridtable', async () => { + config = DEFAULT_CONFIG_EMPTY; + await testRender('description-gridtable', 'head'); + }); + it('uses correct description with blockquote', async () => { config = DEFAULT_CONFIG_EMPTY; await testRender('description-blockquote', 'head');