Skip to content

Commit 9658ed4

Browse files
committed
Turn the corner, cover, and main tpl functions into Solid components with props, then use them as JSX
1 parent fd494bd commit 9658ed4

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

packages/docsify-server-renderer/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import DOMPurify from 'dompurify';
77
import { AbstractHistory } from '../../src/core/router/history/abstract';
88
import { Compiler } from '../../src/core/render/compiler';
99
import { isAbsolutePath } from '../../src/core/router/util';
10-
import * as tpl from '../../src/core/render/tpl';
10+
// eslint-disable-next-line
11+
import { GithubCorner, Cover, Main } from '../../src/core/render/tpl';
1112
import { prerenderEmbed } from '../../src/core/render/embed';
1213

1314
function cwd(...args) {
@@ -47,10 +48,14 @@ function mainTpl(config) {
4748
class={`app-nav${config.repo ? '' : ' no-badge'}`}
4849
innerHTML={'<!--navbar-->'}
4950
></nav>
50-
51-
{config.repo && tpl.corner(config.repo)}
52-
{config.coverpage && tpl.cover()}
53-
{tpl.main(config)}
51+
{config.repo && (
52+
<GithubCorner
53+
githubUrl={config.repo}
54+
cornerExternalLinkTarget={config.cornerExternalLinkTarge}
55+
/>
56+
)}
57+
{config.coverpage && <Cover />}
58+
<Main {...config} />
5459
</>
5560
);
5661
}

src/core/render/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isMobile, inBrowser } from '../util/env';
99
import { isPrimitive, merge } from '../util/core';
1010
import { scrollActiveSidebar } from '../event/scroll';
1111
import { Compiler } from './compiler';
12-
import * as tpl from './tpl';
12+
import { GithubCorner, Cover, Main, Theme } from './tpl';
1313
import { prerenderEmbed } from './embed';
1414

1515
let vueGlobalData;
@@ -422,11 +422,17 @@ export function Render(Base) {
422422

423423
if (el) {
424424
if (config.repo) {
425-
html.push(tpl.corner(config.repo, config.cornerExternalLinkTarge));
425+
html.push(
426+
<GithubCorner
427+
githubUrl={config.repo}
428+
cornerExternalLinkTarget={config.cornerExternalLinkTarget}
429+
/>
430+
);
426431
}
427432

428433
if (config.coverpage) {
429-
html.push(tpl.cover());
434+
// eslint-disable-next-line new-cap
435+
html.push(<Cover />);
430436
}
431437

432438
if (config.logo) {
@@ -439,7 +445,9 @@ export function Render(Base) {
439445
}
440446
}
441447

442-
html.push(tpl.main(config));
448+
// eslint-disable-next-line new-cap
449+
html.push(<Main {...config} />);
450+
443451
// Render main app
444452
this._renderTo(el, html, true);
445453
} else {
@@ -462,7 +470,7 @@ export function Render(Base) {
462470
}
463471

464472
if (config.themeColor) {
465-
dom.$.head.appendChild(tpl.theme(config.themeColor));
473+
dom.$.head.appendChild(<Theme color={config.themeColor} />);
466474
// Polyfll
467475
cssVars(config.themeColor);
468476
}

src/core/render/tpl.js

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import { createMemo } from 'solid-js';
12
/**
23
* Render github corner
3-
* @param {Object} data URL for the View Source on Github link
4-
* @param {String} cornerExternalLinkTarge value of the target attribute of the link
5-
* @return {String} SVG element as string
4+
* @param {Object} props
5+
* @param {string} props.githubUrl URL for the View Source on Github link
6+
* @param {string=} props.cornerExternalLinkTarget value of the target attribute of the link
67
*/
7-
export function corner(data, cornerExternalLinkTarge) {
8-
if (!data) {
9-
return '';
10-
}
8+
export function GithubCorner(props) {
9+
const processedUrl = createMemo(() => {
10+
let result = props.githubUrl;
1111

12-
if (!/\/\//.test(data)) {
13-
data = 'https://github.com/' + data;
14-
}
12+
if (!/\/\//.test(result)) {
13+
result = 'https://github.com/' + result;
14+
}
15+
16+
result = result.replace(/^git\+/, '');
1517

16-
data = data.replace(/^git\+/, '');
17-
// Double check
18-
cornerExternalLinkTarge = cornerExternalLinkTarge || '_blank';
18+
return result;
19+
});
1920

2021
return (
2122
<a
22-
href={data}
23-
target={cornerExternalLinkTarge}
23+
href={processedUrl()}
24+
target={props.cornerExternalLinkTarget || '_blank'}
2425
class="github-corner"
2526
aria-label="View source on Github"
2627
>
@@ -45,9 +46,8 @@ export function corner(data, cornerExternalLinkTarge) {
4546
/**
4647
* Renders main content
4748
* @param {Object} config Configuration object
48-
* @returns {String} HTML of the main content
4949
*/
50-
export function main(config) {
50+
export function Main(config) {
5151
const name = config.name ? config.name : '';
5252

5353
const aside = (
@@ -90,7 +90,7 @@ export function main(config) {
9090
* Cover Page
9191
* @returns {String} Cover page
9292
*/
93-
export function cover() {
93+
export function Cover() {
9494
const SL = ', 100%, 85%';
9595
const bgc =
9696
'linear-gradient(to left bottom, ' +
@@ -104,9 +104,9 @@ export function cover() {
104104
</section>
105105
);
106106

107-
// Bug with Jest/jsdom: at this point, the styles exist, Docsify works
108-
// and this log will show the background value. But only during Jest tests, the
109-
// bakground value is empty. This is why the snapshot
107+
// JEST_JSDOM_BUG: At this point, the styles exist, Docsify works and this log
108+
// will show the background value. But only during Jest tests, the bakground
109+
// value is empty.
110110
// console.log('cover style?', el.style.background);
111111

112112
return el;
@@ -142,11 +142,12 @@ export function markdownParagraph(className, content) {
142142
return `<p class="${className}">${content.slice(5).trim()}</p>`;
143143
}
144144

145-
export function theme(color) {
145+
/** @param {{color: string}} props */
146+
export function Theme(props) {
146147
return (
147148
<style>{/* css */ `
148149
:root {
149-
--theme-color: ${color};
150+
--theme-color: ${props.color};
150151
}
151152
`}</style>
152153
);

src/core/util/dom.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const cacheNode = {};
55

66
/**
77
* Get Node
8-
* @param {String|Element} el A DOM element
9-
* @param {Boolean} noCache Flag to use or not use the cache
10-
* @return {Element} The found node element
8+
* @param {string|Element} el A DOM element
9+
* @param {boolean} noCache Flag to use or not use the cache
10+
* @returns {Element|null} The found node element
1111
*/
1212
export function getNode(el, noCache = false) {
1313
if (typeof el === 'string') {

0 commit comments

Comments
 (0)