Skip to content

Commit

Permalink
breaking: remove markdown pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenjie Xia committed Jan 26, 2021
1 parent d2bf94b commit 8162d2f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 55 deletions.
4 changes: 2 additions & 2 deletions framework/core/routing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DependencyDescriptor } from '../../server/types.ts'
import { reMDExt, reModuleExt } from '../../shared/constants.ts'
import { reModuleExt } from '../../shared/constants.ts'
import util from '../../shared/util.ts'
import type { RouterURL } from '../../types.ts'
import events from './events.ts'
Expand Down Expand Up @@ -172,7 +172,7 @@ export class Routing {
}

export function getPagePath(url: string): string {
const pathname = url.replace(reModuleExt, '').replace(reMDExt, '').toLowerCase().replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
const pathname = url.replace(reModuleExt, '').toLowerCase().replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
return pathname.startsWith('/api/') ? pathname : pathname.replace(/\s+/g, '-')
}

Expand Down
52 changes: 4 additions & 48 deletions server/project.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { initWasm, SWCOptions, TransformOptions, transpileSync } from '../compiler/mod.ts'
import type { AcceptedPlugin, ECMA, ServerRequest } from '../deps.ts'
import { CleanCSS, colors, ensureDir, marked, minify, path, postcss, safeLoadFront, Sha1, Sha256, walk } from '../deps.ts'
import { CleanCSS, colors, ensureDir, minify, path, postcss, Sha1, Sha256, walk } from '../deps.ts'
import { EventEmitter } from '../framework/core/events.ts'
import { getPagePath, RouteModule, Routing } from '../framework/core/routing.ts'
import { hashShort, reFullVersion, reHashJs, reHashResolve, reHttp, reLocaleID, reMDExt, reModuleExt, reStyleModuleExt } from '../shared/constants.ts'
import { hashShort, reFullVersion, reHashJs, reHashResolve, reHttp, reLocaleID, reModuleExt, reStyleModuleExt } from '../shared/constants.ts'
import { cleanupCompilation, ensureTextFile, existsDirSync, existsFileSync } from '../shared/fs.ts'
import log from '../shared/log.ts'
import util from '../shared/util.ts'
Expand Down Expand Up @@ -92,9 +92,6 @@ export class Project {
if (reStyleModuleExt.test(url)) {
return true
}
if (reMDExt.test(url)) {
return url.startsWith('/pages/')
}
if (reModuleExt.test(url)) {
return url.startsWith('/pages/') ||
url.startsWith('/components/') ||
Expand Down Expand Up @@ -280,7 +277,7 @@ export class Project {
`import { createHotContext } from ${JSON.stringify(hmrImportPath)};`,
`import.meta.hot = createHotContext(${JSON.stringify(url)});`
]
const reactRefresh = this.config.framework === 'react' && (reModuleExt.test(url) || reMDExt.test(url))
const reactRefresh = this.config.framework === 'react' && reModuleExt.test(url)
if (reactRefresh) {
const refreshImportPath = getRelativePath(
path.dirname(localUrl),
Expand Down Expand Up @@ -630,7 +627,7 @@ export class Project {
}
}
}
if (path.startsWith('/pages/') && (reModuleExt.test(path) || reMDExt.test(path))) {
if (path.startsWith('/pages/') && reModuleExt.test(path)) {
return true
}
let isDep = false
Expand Down Expand Up @@ -968,47 +965,6 @@ export class Project {
})
cssMod.loader = 'css'
return cssMod
} else if (loader === 'markdown') {
this.#modules.delete(url)
const { __content, ...props } = safeLoadFront(sourceCode)
const html = marked.parse(__content)
const mdMod = await this.compile(url, {
...options,
loader: 'js',
sourceCode: (`
import React, { useEffect, useRef } from "https://esm.sh/react@${this.config.reactVersion}";
import { redirect } from "${alephPkgUrl}/framework/${this.config.framework}/anchor.ts";
export default function MarkdownPage() {
const ref = useRef(null);
useEffect(() => {
const anchors = [];
const onClick = e => {
e.preventDefault();
redirect(e.currentTarget.getAttribute("href"));
};
if (ref.current) {
ref.current.querySelectorAll("a").forEach(a => {
const href = a.getAttribute("href");
if (href && !/^[a-z0-9]+:/i.test(href)) {
a.addEventListener("click", onClick, false);
anchors.push(a);
}
});
}
return () => anchors.forEach(a => a.removeEventListener("click", onClick));
}, []);
return React.createElement("div", {
className: "markdown-page",
ref,
dangerouslySetInnerHTML: {__html: ${JSON.stringify(html)}}
});
}
MarkdownPage.meta = ${JSON.stringify(props, undefined, 4)};
`).replaceAll(' '.repeat(24), '').trim(),
rawSourceHash: (new Sha1).update(sourceCode).hex()
})
mdMod.loader = 'js'
return mdMod
} else if (loader === 'js' || loader === 'ts' || loader === 'jsx' || loader === 'tsx') {
const t = performance.now()
const swcOptions: SWCOptions = {
Expand Down
6 changes: 2 additions & 4 deletions server/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { colors, path } from '../deps.ts'
import { MB, reHttp, reMDExt, reModuleExt, reStyleModuleExt } from '../shared/constants.ts'
import { MB, reHttp, reModuleExt, reStyleModuleExt } from '../shared/constants.ts'
import log from '../shared/log.ts'
import util from '../shared/util.ts'
import type { ServerRequest } from '../types.ts'
Expand Down Expand Up @@ -61,8 +61,6 @@ export function newModule(url: string): Module {
let loader = ''
if (reStyleModuleExt.test(url)) {
loader = 'css'
} else if (reMDExt.test(url)) {
loader = 'markdown'
} else if (reModuleExt.test(url)) {
loader = url.split('.').pop()!
if (loader === 'mjs') {
Expand Down Expand Up @@ -119,7 +117,7 @@ export function fixImportUrl(importUrl: string): string {
const isRemote = reHttp.test(importUrl)
const url = new URL(isRemote ? importUrl : 'file://' + importUrl)
let ext = path.extname(path.basename(url.pathname)) || '.js'
if (isRemote && !reModuleExt.test(ext) && !reStyleModuleExt.test(ext) && !reMDExt.test(ext)) {
if (isRemote && !reModuleExt.test(ext) && !reStyleModuleExt.test(ext)) {
ext = '.js'
}
let pathname = util.trimSuffix(url.pathname, ext)
Expand Down
1 change: 0 additions & 1 deletion shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const hashShort = 9
export const reHttp = /^https?:\/\//i
export const reModuleExt = /\.(js|jsx|mjs|ts|tsx)$/i
export const reStyleModuleExt = /\.(pcss|css)$/i
export const reMDExt = /\.(md|markdown)$/i
export const reLocaleID = /^[a-z]{2}(-[a-zA-Z0-9]+)?$/
export const reFullVersion = /@v?\d+\.\d+\.\d+/i
export const reHashJs = new RegExp(`\\.[0-9a-fx]{${hashShort}}\\.js$`, 'i')
Expand Down

0 comments on commit 8162d2f

Please sign in to comment.