|
1 | | -// @deno-types="https://deno.land/x/types/react/v16.13.1/react.d.ts" |
2 | | - |
3 | | -import Sidebar from './_sidebar.js'; |
4 | | -const Layout = ({ config, title, content, ga, gitalk, script, sidebar, outputPath }) => { |
5 | | - const [isDark, setIsDark] = React.useState( |
6 | | - // @ts-ignore |
7 | | - window.Deno ? false : document.documentElement.classList.contains('is_dark')); |
8 | | - return (React.createElement("html", { className: isDark ? 'is_dark' : '' }, |
9 | | - React.createElement("head", null, |
10 | | - ga, |
11 | | - React.createElement("title", null, outputPath !== 'index.html' ? `${title} · ${config.title}` : title), |
12 | | - React.createElement("meta", { charSet: "utf-8" }), |
13 | | - React.createElement("link", { id: "prismTheme", rel: "stylesheet", href: isDark ? `${config.base}assets/prism_tomorrow.css` : `${config.base}assets/prism.css` }), |
14 | | - React.createElement("script", { dangerouslySetInnerHTML: { |
15 | | - __html: ` |
16 | | -let shouldSetIsDark = document.cookie.includes('is_dark=1') ? true : document.cookie.includes('is_dark=0') ? false : window.matchMedia('(prefers-color-scheme: dark)').matches |
17 | | -if (shouldSetIsDark) { |
18 | | - document.documentElement.classList.add('is_dark'); |
19 | | - document.getElementById('prismTheme').href = "${config.base}assets/prism_tomorrow.css"; |
| 1 | +import * as path from 'https://deno.land/[email protected]/path/mod.js'; |
| 2 | +import * as fs from 'https://deno.land/[email protected]/fs/mod.js'; |
| 3 | +import { green } from 'https://deno.land/[email protected]/fmt/colors.js'; |
| 4 | +import * as ts from 'https://dev.jspm.io/[email protected]'; |
| 5 | +/** |
| 6 | + * Compile input code from tsx to js, by typescript compiler |
| 7 | + * Will replace `.tsx` to `.js` in `import` statement, and remove react and react-dom imports |
| 8 | + */ |
| 9 | +export function compile(input) { |
| 10 | + return ts.default |
| 11 | + .transpileModule(input, { |
| 12 | + compilerOptions: { |
| 13 | + target: 'ESNext', |
| 14 | + module: 'ESNext', |
| 15 | + jsx: 'React', |
| 16 | + removeComments: false, |
| 17 | + newLine: 'lf' |
| 18 | + } |
| 19 | + }) |
| 20 | + .outputText.replace(/(^import .*)\.tsx?((?:'|");?$)/gm, '$1.js$2') |
| 21 | + .replace(/(^import .*)\/react(\/|'|"|@).*$/gm, '') |
| 22 | + .replace(/(^import .*)\/react-dom(\/|'|"|@).*$/gm, ''); |
| 23 | +} |
| 24 | +/** |
| 25 | + * Read input file and then compile it |
| 26 | + */ |
| 27 | +export async function compileFile(src) { |
| 28 | + console.log(green('Compile file'), src); |
| 29 | + const content = await fs.readFileStr(src); |
| 30 | + return compile(content); |
| 31 | +} |
| 32 | +/** |
| 33 | + * Compile a pagic file with local or remote url |
| 34 | + */ |
| 35 | +export async function compilePagicFile(pathToPagicRoot) { |
| 36 | + console.log(green('Compile pagic file'), pathToPagicRoot); |
| 37 | + let content = ''; |
| 38 | + if (import.meta.url.startsWith('file://')) { |
| 39 | + const src = path.resolve(path.dirname(path.fromFileUrl(import.meta.url)), '../../', pathToPagicRoot); |
| 40 | + content = await fs.readFileStr(src); |
| 41 | + } |
| 42 | + else { |
| 43 | + const res = await fetch(import.meta.url.replace(/\/src\/utils\/mod\.ts$/, `/${pathToPagicRoot}`)); |
| 44 | + content = await res.text(); |
| 45 | + } |
| 46 | + return compile(content); |
20 | 47 | } |
21 | | -` |
22 | | - } }), |
23 | | - React.createElement("link", { rel: "stylesheet", href: `${config.base}assets/index.css` })), |
24 | | - React.createElement("body", null, |
25 | | - React.createElement("header", null, |
26 | | - React.createElement("h1", null, |
27 | | - React.createElement("a", { href: config.base }, config.title)), |
28 | | - React.createElement("nav", null, |
29 | | - React.createElement("ul", null, |
30 | | - config.nav.map(({ text, link }) => (React.createElement("li", { key: link }, |
31 | | - React.createElement("a", { href: link }, text)))), |
32 | | - React.createElement("li", null, |
33 | | - React.createElement("a", { href: "#", onClick: (e) => { |
34 | | - e.preventDefault(); |
35 | | - setIsDark(!isDark); |
36 | | - // @ts-ignore |
37 | | - document.cookie = `is_dark=${!isDark ? '1' : '0'}; expires=Tue, 19 Jun 2038 03:14:07 UTC; path=/`; |
38 | | - } }, isDark ? '关闭黑暗模式' : '开启黑暗模式'))))), |
39 | | - React.createElement(Sidebar, { sidebar: sidebar, outputPath: outputPath, config: config }), |
40 | | - React.createElement("section", { className: "main" }, |
41 | | - content, |
42 | | - gitalk), |
43 | | - script))); |
44 | | -}; |
45 | | -export default Layout; |
|
0 commit comments