-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb27a2b
commit dd09995
Showing
40 changed files
with
276 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Meta } from "#/lib/meta/mod.ts"; | ||
import Replace from "#/components/replace.tsx"; | ||
import Playground from "#/components/playground/playground.tsx"; | ||
import { | ||
getDataByRegExpMatchArray, | ||
PLAYGROUND_EXPRESSION_REGEX, | ||
} from "./expression.ts"; | ||
|
||
/** | ||
* DocProps are the properties for the Doc component. | ||
*/ | ||
export interface DocProps { | ||
/** | ||
* html is the html content of the documentation page. | ||
*/ | ||
html: string; | ||
|
||
/** | ||
* meta is the meta data for the documentation page's playgrounds. | ||
*/ | ||
meta: Meta; | ||
} | ||
|
||
/** | ||
* Doc is a jsonx documentation page component. | ||
*/ | ||
export default function Doc(props: DocProps) { | ||
// TODO: Replace with actual MDX parsing because the async isn't working | ||
// with Preact hydration. | ||
return ( | ||
<Replace | ||
html={props.html} | ||
pattern={PLAYGROUND_EXPRESSION_REGEX} | ||
component={async ({ match }) => { | ||
const data = await getDataByRegExpMatchArray(match); | ||
return ( | ||
<Playground | ||
meta={props.meta} | ||
code={data.code} | ||
version={data.version} | ||
/> | ||
); | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @fileoverview | ||
* | ||
* This file contains the helper functions for parsing and evaluating playground | ||
* expressions. Playground expressions are a simple syntax for embedding | ||
* playgrounds directly into markdown files. | ||
*/ | ||
|
||
import { getPlayground } from "#/lib/playgrounds/deno_kv/mod.ts"; | ||
import { kv } from "#/lib/resources/kv.ts"; | ||
import { readExample } from "#/lib/examples/mod.ts"; | ||
|
||
/** | ||
* PLAYGROUND_EXPRESSION_REGEX is the regular expression for parsing playground | ||
* expressions. | ||
*/ | ||
export const PLAYGROUND_EXPRESSION_REGEX = | ||
/<!--\s*playground\s+(id|example):[a-zA-Z0-9-_.]+\s*-->/g; | ||
|
||
/** | ||
* fromRegExpMatchArray converts a regular expression match array to a | ||
* PlaygroundExpression. | ||
*/ | ||
export function fromRegExpMatchArray( | ||
match: RegExpMatchArray, | ||
): PlaygroundExpression { | ||
return parsePlaygroundExpression(match[0]); | ||
} | ||
|
||
/** | ||
* PlaygroundExpression is a playground expression. | ||
*/ | ||
export type PlaygroundExpression = | ||
| { id: string } | ||
| { example: string }; | ||
|
||
/** | ||
* parsePlaygroundExpression parses a playground expression. | ||
* | ||
* Example playground expressions: | ||
* | ||
* <!-- playground id:abc123 --> | ||
* <!-- playground example:hello_world --> | ||
*/ | ||
export function parsePlaygroundExpression( | ||
expression: string, | ||
): PlaygroundExpression { | ||
const idMatch = expression.match(/id:([a-zA-Z0-9-_.]+)/); | ||
if (idMatch) { | ||
return { id: idMatch[1] }; | ||
} | ||
|
||
const exampleMatch = expression.match(/example:([a-zA-Z0-9-_.]+)/); | ||
if (exampleMatch) { | ||
return { example: exampleMatch[1] }; | ||
} | ||
|
||
throw new Error(`Invalid playground expression: ${expression}`); | ||
} | ||
|
||
/** | ||
* getDataByRegExpMatchArray converts a regular expression match array to | ||
* playground data. | ||
*/ | ||
export async function getDataByRegExpMatchArray( | ||
match: RegExpMatchArray, | ||
): Promise<{ code: string; version?: string }> { | ||
const expr = fromRegExpMatchArray(match); | ||
console.log({ expr }); | ||
if ("id" in expr) { | ||
const playground = await getPlayground(kv, expr.id); | ||
if (!playground) { | ||
throw new Error(`Playground not found: ${expr.id}`); | ||
} | ||
|
||
return { code: playground.code, version: playground.version }; | ||
} | ||
|
||
if ("example" in expr) { | ||
const example = await readExample(`./examples/${expr.example}`); | ||
if (!example) { | ||
throw new Error(`Example not found: ${expr.example}`); | ||
} | ||
|
||
return { code: example }; | ||
} | ||
|
||
throw new Error(`Invalid playground expression: ${expr}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { JSX } from "preact/jsx-runtime"; | ||
|
||
/** | ||
* ReplaceProps | ||
*/ | ||
export interface ReplaceProps { | ||
/** | ||
* html is the html content to be replaced. | ||
*/ | ||
html: string; | ||
|
||
/** | ||
* pattern is the pattern to inject the component by. | ||
*/ | ||
pattern: RegExp; | ||
|
||
/** | ||
* component is the component to be injected in place of the content. | ||
*/ | ||
component: ( | ||
props: { match: RegExpMatchArray }, | ||
) => JSX.Element | Promise<JSX.Element>; | ||
} | ||
|
||
/** | ||
* Replace is a component that replaces the content of the element with the content of the component. | ||
*/ | ||
export default function Replace(props: ReplaceProps) { | ||
const segments = props.html.split(props.pattern); | ||
if (segments.length === 1) { | ||
return <Raw>{props.html}</Raw>; | ||
} | ||
|
||
const matches = [...props.html.matchAll(props.pattern)]; | ||
return ( | ||
<> | ||
{segments.map(async (segment, index) => ( | ||
<> | ||
<Raw>{segment}</Raw> | ||
{index < matches.length && | ||
await props.component({ match: matches[index] })} | ||
</> | ||
))} | ||
</> | ||
); | ||
} | ||
|
||
function Raw(props: { children: string }) { | ||
return <div dangerouslySetInnerHTML={{ __html: props.children }}></div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
title: API Docs | ||
href: https://jsr.io/@fartlabs/jsonx | ||
--- | ||
--- |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./items.ts"; | ||
export * from "./nodes.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./mod_client.ts"; | ||
export * from "./fs.ts"; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* readExample reads the example file and returns the content. | ||
*/ | ||
export async function readExample(path: string | URL): Promise<string | null> { | ||
try { | ||
const text = await Deno.readTextFile(path); | ||
return trimJSXImportSource(text); | ||
} catch (error) { | ||
if (error instanceof Deno.errors.NotFound) { | ||
return null; | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
function trimJSXImportSource(code: string): string { | ||
const jsxImportSource = "/** @jsxImportSource @fartlabs/jsonx */\n\n"; | ||
if (code.startsWith(jsxImportSource)) { | ||
return code.substring(jsxImportSource.length); | ||
} | ||
|
||
return code; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./meta.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./kv.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./playgrounds.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { readFSItems } from "#/lib/docs/mod_server.ts"; | ||
|
||
export const { items, contents, nodes } = await readFSItems({ | ||
root: "./docs", | ||
isIndex: (suffix) => suffix.startsWith("00_"), | ||
renderOptions: { | ||
// Preserve HTML comments as-is. | ||
disableHtmlSanitization: true, | ||
// | ||
// TODO: Define the base URL for the site with environment variables. | ||
// | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { readExample } from "#/lib/examples/examples.ts"; | ||
|
||
export const defaultExample = (await readExample("./examples/01_animals.tsx"))!; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { Handlers } from "$fresh/server.ts"; | ||
import { getExampleByName } from "#/server/examples/mod.ts"; | ||
import { readExample } from "#/lib/examples/mod.ts"; | ||
|
||
export const handler: Handlers = { | ||
async GET(_request, ctx) { | ||
const example = await getExampleByName(ctx.params.id); | ||
const example = await readExample(`./examples/${ctx.params.name}`); | ||
return new Response(example); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
dd09995
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to deploy: