From bd5f879cb859c024169ba621683c184fe1c20be2 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Mon, 13 May 2024 22:40:53 +0800 Subject: [PATCH 1/7] feat: The timing of the `doneEach` hook call for the cover The `doneEach` hook function for the cover should be called after the cover page HTML has been appended to the DOM. --- src/core/fetch/index.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 52c777ff9..60f7a25ae 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -150,7 +150,7 @@ export function Fetch(Base) { } } - _fetchCover() { + _fetchCover(cb = noop) { const { coverpage, requestHeaders } = this.config; const query = this.route.query; const root = getParentPath(this.route.path); @@ -174,13 +174,17 @@ export function Fetch(Base) { path = this.router.getFile(root + path); this.coverIsHTML = /\.html$/g.test(path); get(path + stringifyQuery(query, ['id']), false, requestHeaders).then( - text => this._renderCover(text, coverOnly) + text => { + this._renderCover(text, coverOnly); + cb(coverOnly); + } ); } else { this._renderCover(null, coverOnly); + cb(coverOnly); } - - return coverOnly; + } else { + cb(false); } } @@ -190,16 +194,16 @@ export function Fetch(Base) { cb(); }; - const onlyCover = this._fetchCover(); - - if (onlyCover) { - done(); - } else { - this._fetch(() => { - onNavigate(); + this._fetchCover(onlyCover => { + if (onlyCover) { done(); - }); - } + } else { + this._fetch(() => { + onNavigate(); + done(); + }); + } + }); } _fetchFallbackPage(path, qs, cb = noop) { From dea1229ea18333f37cb0bb07dad6e7f519366c56 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Wed, 29 May 2024 17:21:44 +0800 Subject: [PATCH 2/7] test: Add test cases for the `doneEach` hook --- test/e2e/plugins.test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/e2e/plugins.test.js b/test/e2e/plugins.test.js index 68534830d..ee3522685 100644 --- a/test/e2e/plugins.test.js +++ b/test/e2e/plugins.test.js @@ -158,6 +158,36 @@ test.describe('Plugins', () => { }); }); + test.describe('doneEach()', () => { + test('callback after cover loads', async ({ page }) => { + const consoleMessages = []; + + page.on('console', msg => consoleMessages.push(msg.text())); + + await docsifyInit({ + config: { + plugins: [ + function (hook) { + hook.doneEach(() => { + const homepageTitle = document.querySelector('#homepage-title'); + const coverTitle = document.querySelector('#cover-title'); + console.log(homepageTitle?.textContent); + console.log(coverTitle?.textContent); + }); + }, + ], + }, + markdown: { + homepage: '# Hello World :id=homepage-title', + coverpage: '# Cover Page :id=cover-title', + }, + // _logHTML: true, + }); + + await expect(consoleMessages).toEqual(['Hello World', 'Cover Page']); + }); + }); + test.describe('route data accessible to plugins', () => { let routeData = null; From 56c922fe43c3e24e369bd30bf2dd9ee1ef500d6a Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Wed, 29 May 2024 17:22:08 +0800 Subject: [PATCH 3/7] style: Fix eslint code style --- src/core/fetch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index e296141ed..73cb54c81 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -177,7 +177,7 @@ export function Fetch(Base) { text => { this._renderCover(text, coverOnly); cb(coverOnly); - } + }, ); } else { this._renderCover(null, coverOnly); From 837d04af17e7eb4d0532bf02d9005c8c2433841e Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Thu, 30 May 2024 15:13:56 +0800 Subject: [PATCH 4/7] fix(test): fix HTML logging --- test/helpers/docsify-init.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/helpers/docsify-init.js b/test/helpers/docsify-init.js index bbc357022..de406f527 100644 --- a/test/helpers/docsify-init.js +++ b/test/helpers/docsify-init.js @@ -363,13 +363,13 @@ async function docsifyInit(options = {}) { } if (htmlArr.length) { - htmlArr.forEach(html => { + for (let html of htmlArr) { if (settings._logHTML.format !== false) { - html = prettier.format(html, { parser: 'html' }); + html = await prettier.format(html, { parser: 'html' }); } console.log(html); - }); + } } else { console.warn(`docsify-init(): unable to match selector '${selector}'`); } From ec48f93351b521057a1f9e457c5648bd72bef809 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Thu, 30 May 2024 16:51:39 +0800 Subject: [PATCH 5/7] feat(test): add dynamic and asynchronous content provision support for test instances --- test/helpers/docsify-init.js | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/test/helpers/docsify-init.js b/test/helpers/docsify-init.js index de406f527..6fd4f5d0c 100644 --- a/test/helpers/docsify-init.js +++ b/test/helpers/docsify-init.js @@ -17,11 +17,11 @@ const docsifyURL = '/dist/docsify.js'; // Playwright * @param {Function|Object} [options.config] docsify configuration (merged with default) * @param {String} [options.html] HTML content to use for docsify `index.html` page * @param {Object} [options.markdown] Docsify markdown content - * @param {String} [options.markdown.coverpage] coverpage markdown - * @param {String} [options.markdown.homepage] homepage markdown - * @param {String} [options.markdown.navbar] navbar markdown - * @param {String} [options.markdown.sidebar] sidebar markdown - * @param {Object} [options.routes] custom routes defined as `{ pathOrGlob: responseText }` + * @param {String|(()=>Promise|String)} [options.markdown.coverpage] coverpage markdown + * @param {String|(()=>Promise|String)} [options.markdown.homepage] homepage markdown + * @param {String|(()=>Promise|String)} [options.markdown.navbar] navbar markdown + * @param {String|(()=>Promise|String)} [options.markdown.sidebar] sidebar markdown + * @param {RecordPromise|String)>} [options.routes] custom routes defined as `{ pathOrGlob: response }` * @param {String} [options.script] JS to inject via