Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 6b27e40

Browse files
author
Je
committed
refactor: hijack markdown page inner link 'click' event
1 parent 1a5dc95 commit 6b27e40

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

project.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -792,36 +792,50 @@ export class Project {
792792
const output = cleanCSS.minify(css)
793793
css = output.styles
794794
}
795-
const hash = (new Sha1).update(css).hex()
796-
const filepath = path.join(
797-
path.dirname(mod.sourceFilePath),
798-
util.trimSuffix(path.basename(mod.sourceFilePath), '.css') + '.' + hash.slice(0, hashShort) + '.css'
799-
)
800-
const asLink = css.length > 1024
801-
if (asLink) {
802-
await writeTextFile(path.join(this.buildDir, filepath), css)
803-
}
804795
mod.jsContent = [
805796
`import { applyCSS } from ${JSON.stringify(relativePath(
806797
path.dirname(mod.url),
807798
'/-/deno.land/x/aleph/head.js'
808799
))};`,
809-
`applyCSS(${JSON.stringify(url)}, ${asLink ? JSON.stringify(path.join(this.config.baseUrl, '_aleph', filepath)) + ', true' : JSON.stringify(this.isDev ? `\n${css}\n` : css)});`,
800+
`applyCSS(${JSON.stringify(url)}, ${JSON.stringify(this.isDev ? `\n${css}\n` : css)});`,
810801
].join(this.isDev ? '\n' : '')
811802
mod.jsSourceMap = ''
812-
mod.hash = hash
803+
mod.hash = (new Sha1).update(css).hex()
813804
} else if (mod.sourceType === 'sass' || mod.sourceType === 'scss') {
814805
// todo: support sass
815-
} else if (mod.sourceType === 'md' || mod.sourceType === 'mdx') {
806+
} else if (mod.sourceType === 'mdx') {
807+
// todo: support mdx
808+
} else if (mod.sourceType === 'md' || mod.sourceType === 'markdown') {
816809
const { __content, ...props } = safeLoadFront(sourceContent)
817810
const html = marked.parse(__content)
818811
mod.jsContent = [
819-
`import React from ${JSON.stringify(relativePath(path.dirname(mod.sourceFilePath), '/-/esm.sh/react.js'))};`,
820-
`export default function Markdown() {`,
821-
` return React.createElement("div", ${JSON.stringify({ className: 'markdown', dangerouslySetInnerHTML: { __html: html } })});`,
812+
this.isDev && `const _s = $RefreshSig$();`,
813+
`import React, { useEffect, useRef } from ${JSON.stringify(relativePath(path.dirname(mod.sourceFilePath), '/-/esm.sh/react.js'))};`,
814+
`import { redirect } from ${JSON.stringify(relativePath(path.dirname(mod.sourceFilePath), '/-/deno.land/x/aleph/aleph.js'))};`,
815+
`export default function MarkdownPage() {`,
816+
this.isDev && ` _s();`,
817+
` const ref = useRef(null);`,
818+
` useEffect(() => {`,
819+
` const appLinks = [];`,
820+
` const onClick = e => {`,
821+
` e.preventDefault();`,
822+
` redirect(e.target.getAttribute("href"));`,
823+
` };`,
824+
` if (ref.current) {`,
825+
` ref.current.querySelectorAll("a").forEach(a => {`,
826+
` if (!/^(https?|mailto|file):/i.test(a.getAttribute("href"))) {`,
827+
` a.addEventListener("click", onClick);`,
828+
` appLinks.push(a);`,
829+
` }`,
830+
` });`,
831+
` }`,
832+
` return () => appLinks.forEach(a => a.removeEventListener("click", onClick));`,
833+
` }, [])`,
834+
` return React.createElement("div", {className: "markdown-page", ref, dangerouslySetInnerHTML: {__html: ${JSON.stringify(html)}}});`,
822835
`}`,
823-
`Markdown.meta = ${JSON.stringify(props, undefined, this.isDev ? 4 : undefined)};`,
824-
this.isDev && `$RefreshReg$(Markdown, "Markdown");`,
836+
`MarkdownPage.meta = ${JSON.stringify(props, undefined, this.isDev ? 4 : undefined)};`,
837+
this.isDev && `_s(MarkdownPage, "useRef{ref}\\nuseEffect{}");`,
838+
this.isDev && `$RefreshReg$(MarkdownPage, "MarkdownPage");`,
825839
].filter(Boolean).join(this.isDev ? '\n' : '')
826840
mod.jsSourceMap = ''
827841
mod.hash = (new Sha1).update(mod.jsContent).hex()

0 commit comments

Comments
 (0)