8000 feat: The timing of the `doneEach` hook call for the cover by YiiGuxing · Pull Request #2427 · docsifyjs/docsify · GitHub
[go: up one dir, main page]

Skip to content

feat: The timing of the doneEach hook call for the cover #2427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
10000
Diff view
Prev Previous commit
Next Next commit
feat: cover page supports embedding external files
  • Loading branch information
YiiGuxing committed May 30, 2024
commit 7cc7cdf0cb7c61baa9ddd4dd2bf190f5dad9db5b
15 changes: 10 additions & 5 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,23 @@ export function Fetch(Base) {
}

const coverOnly = Boolean(path) && this.config.onlyCover;
const next = () => cb(coverOnly);
if (path) {
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);
cb(coverOnly);
text => this._renderCover(text, coverOnly, next),
(event, response) => {
this.coverIsHTML = false;
this._renderCover(
`# ${response.status} - ${response.statusText}`,
coverOnly,
next,
);
},
);
} else {
this._renderCover(null, coverOnly);
cb(coverOnly);
this._renderCover(null, coverOnly, next);
}
} else {
cb(false);
Expand Down
57 changes: 38 additions & 19 deletions src/core/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export function Render(Base) {
});
}

_renderCover(text, coverOnly) {
_renderCover(text, coverOnly, next) {
const el = dom.getNode('.cover');

dom.toggleClass(
Expand All @@ -392,37 +392,56 @@ export function Render(Base) {
);
if (!text) {
dom.toggleClass(el, 'remove', 'show');
next();
return;
}

dom.toggleClass(el, 'add', 'show');

let html = this.coverIsHTML ? text : this.compiler.cover(text);
const callback = html => {
const m = html
.trim()
.match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$');

const m = html
.trim()
.match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$');
if (m) {
if (m[2] === 'color') {
el.style.background = m[1] + (m[3] || '');
} else {
let path = m[1];

if (m) {
if (m[2] === 'color') {
el.style.background = m[1] + (m[3] || '');
} else {
let path = m[1];
dom.toggleClass(el, 'add', 'has-mask');
if (!isAbsolutePath(m[1])) {
path = getPath(this.router.getBasePath(), m[1]);
}

dom.toggleClass(el, 'add', 'has-mask');
if (!isAbsolutePath(m[1])) {
path = getPath(this.router.getBasePath(), m[1]);
el.style.backgroundImage = `url(${path})`;
el.style.backgroundSize = 'cover';
el.style.backgroundPosition = 'center center';
}

el.style.backgroundImage = `url(${path})`;
el.style.backgroundSize = 'cover';
el.style.backgroundPosition = 'center center';
html = html.replace(m[0], '');
}

html = html.replace(m[0], '');
}
this._renderTo('.cover-main', html);
next();
};

this._renderTo('.cover-main', html);
// TODO: Call the 'beforeEach' and 'afterEach' hooks.
// However, when the cover and the home page are on the same page,
// the 'beforeEach' and 'afterEach' hooks are called multiple times.
// It is difficult to determine the target of the hook within the
// hook functions. We might need to make some changes.
if (this.coverIsHTML) {
callback(text);
} else {
prerenderEmbed(
{
compiler: this.compiler,
raw: text,
},
tokens => callback(this.compiler.cover(tokens)),
);
}
}

_updateRender() {
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/embed-files.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import docsifyInit from '../helpers/docsify-init.js';
import { test, expect } from './fixtures/docsify-init-fixture.js';

test.describe('Embed files', () => {
const routes = {
'fragment.md': '## Fragment',
};

test('embed into homepage', async ({ page }) => {
await docsifyInit({
routes,
markdown: {
homepage: "# Hello World\n\n[fragment](fragment.md ':include')",
},
_logHTML: {},
});

await expect(page.locator('#main')).toContainText('Fragment');
});

test('embed into cover', async ({ page }) => {
await docsifyInit({
routes,
markdown: {
homepage: '# Hello World',
coverpage: "# Cover\n\n[fragment](fragment.md ':include')",
},
_logHTML: {},
});

await expect(page.locator('.cover-main')).toContainText('Fragment');
});
});
42 changes: 40 additions & 2 deletions test/e2e/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,51 @@ test.describe('Plugins', () => {
},
markdown: {
homepage: '# Hello World :id=homepage-title',
coverpage: '# Cover Page :id=cover-title',
coverpage: () => {
return new Promise(resolve => {
setTimeout(() => resolve('# Cover Page :id=cover-title'), 500);
});
},
},
// _logHTML: true,
// _logHTML: {},
});

await expect(consoleMessages).toEqual(['Hello World', 'Cover Page']);
});

test('only cover', async ({ page }) => {
const consoleMessages = [];

page.on('console', msg => consoleMessages.push(msg.text()));

await docsifyInit({
config: {
onlyCover: true,
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: () => {
return new Promise(resolve => {
setTimeout(() => resolve('# Cover Page :id=cover-title'), 500);
});
},
},
waitForSelector: '.cover-main > *:first-child',
// _logHTML: {},
});

await expect(consoleMessages).toEqual(['undefined', 'Cover Page']);
});
});

test.describe('route data accessible to plugins', () => {
Expand Down
0