From 73bf2300bb0c242bc52147c742de02d14bad17c0 Mon Sep 17 00:00:00 2001 From: Simon Legander Date: Fri, 9 Jul 2021 20:37:11 +0200 Subject: [PATCH 1/2] [feat] Ability to specify group name without modifying id. --- src/parse/page.js | 19 +++++++++++--- test/unit/parse/page.test.js | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/parse/page.js b/src/parse/page.js index 153b3775..bd534699 100644 --- a/src/parse/page.js +++ b/src/parse/page.js @@ -1,13 +1,24 @@ const sleep = require('./sleep') -function page(node, result) { +function pageName(node) { const nameParts = [] - node.id && nameParts.push(node.id) - node.title && nameParts.push(node.title) + if (node.name) { + nameParts.push(node.name) + } else if (node.id) { + nameParts.push(node.id) + } + + if (node.title) { + nameParts.push(node.title) + } + return nameParts.join(' - ') +} + +function page(node, result) { const spec = { - name: nameParts.join(' - '), + name: pageName(node), } if (node.comment) { diff --git a/test/unit/parse/page.test.js b/test/unit/parse/page.test.js index c344dbe6..df116adf 100644 --- a/test/unit/parse/page.test.js +++ b/test/unit/parse/page.test.js @@ -18,6 +18,54 @@ test('main', (t) => { ) }) +test('favor name before title', (t) => { + const result = makeResult() + page({ id: 'page_id', name: 'Page name', title: 'Page title' }, result) + t.deepEqual( + result.pages, + new Map([ + [ + 'page_id', + { + name: 'Page name - Page title', + }, + ], + ]) + ) +}) + +test('fallback to using id as name', (t) => { + const result = makeResult() + page({ id: 'page_id_1', title: 'Page title' }, result) + + const result2 = makeResult() + page({ id: 'page_id_2', name: '', title: 'Page title' }, result2) + + t.deepEqual( + result.pages, + new Map([ + [ + 'page_id_1', + { + name: 'page_id_1 - Page title', + }, + ], + ]) + ) + + t.deepEqual( + result2.pages, + new Map([ + [ + 'page_id_2', + { + name: 'page_id_2 - Page title', + }, + ], + ]) + ) +}) + test('comment', (t) => { const result = makeResult() page({ id: 'page1', title: 'Page 1', comment: 'Heavy load' }, result) From 818f692372cf26178dd4d8e8c219dd9b3c4b9f47 Mon Sep 17 00:00:00 2001 From: Simon Legander Date: Mon, 12 Jul 2021 09:27:12 +0200 Subject: [PATCH 2/2] [fix] Name type checking validation rule. --- src/validate/page.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/validate/page.js b/src/validate/page.js index 6b681540..51dec670 100644 --- a/src/validate/page.js +++ b/src/validate/page.js @@ -41,6 +41,12 @@ function validate(node, i, assay) { `Invalid page title (${i}): must be string` ) } + if (!empty(node.name) && typeof node.name !== 'string') { + throw new InvalidArchiveError( + { name: 'InvalidPageName' }, + `Invalid page name (${i}): must be string` + ) + } if (node.sleep && !Array.isArray(node.sleep)) { throw new InvalidArchiveError( { name: 'InvalidPageSleep' },