-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Handlebars app Signed-off-by: Marcos Candeia <marrcooos@gmail.com> * Add handlebars app Signed-off-by: Marcos Candeia <marrcooos@gmail.com> --------- Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
- Loading branch information
Showing
8 changed files
with
146 additions
and
3 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
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 { default } from "../../handlebars/mod.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,12 @@ | ||
// DO NOT EDIT. This file is generated by deco. | ||
// This file SHOULD be checked into source version control. | ||
// This file is automatically updated during development when running `dev.ts`. | ||
|
||
const manifest = { | ||
"name": "handlebars", | ||
"baseUrl": import.meta.url, | ||
}; | ||
|
||
export type Manifest = typeof manifest; | ||
|
||
export default manifest; |
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,82 @@ | ||
import type { App, AppContext as AC } from "$live/mod.ts"; | ||
import HTMLRenderer from "deco-sites/std/components/HTMLRenderer.tsx"; | ||
import { SourceMap } from "deco/blocks/app.ts"; | ||
import { SectionModule } from "deco/blocks/section.ts"; | ||
import { JSONSchema7 } from "deco/deps.ts"; | ||
import { BlockModuleRef } from "deco/engine/block.ts"; | ||
import Handlebars from "https://esm.sh/v131/handlebars@4.7.8"; | ||
import manifest, { Manifest } from "./manifest.gen.ts"; | ||
|
||
const h = Handlebars as unknown as typeof Handlebars["export="]; | ||
export interface HandlebarSection { | ||
name: string; | ||
/** | ||
* @format html | ||
*/ | ||
content: string; | ||
} | ||
|
||
const getHBVars = (content: string) => { | ||
const ast = h.parse(content); | ||
const keys: Record<string, boolean> = {}; | ||
|
||
for (const i in ast.body) { | ||
if (ast.body[i].type === "MustacheStatement") { | ||
keys[ | ||
(ast.body[i] as unknown as { path: { original: string } }).path.original | ||
] = true; | ||
} | ||
} | ||
return Object.keys(keys); | ||
}; | ||
export interface State { | ||
sections: HandlebarSection[]; | ||
} | ||
/** | ||
* @title Build your own Sections | ||
*/ | ||
export default function App( | ||
state: State, | ||
): App<Manifest, State> { | ||
const [sections, sourceMap] = (state.sections ?? []).reduce( | ||
([currentSections, currentSourceMap], sec) => { | ||
const sectionName = `handlebars/sections/${sec.name}.tsx`; | ||
const compiled = h.compile(sec.content); | ||
const properties: Record<string, JSONSchema7> = {}; | ||
const vars = getHBVars(sec.content); | ||
for (const key of vars) { | ||
properties[key] = { | ||
type: "string", | ||
}; | ||
} | ||
return [{ | ||
...currentSections, | ||
[sectionName]: { | ||
default: (props) => { | ||
return HTMLRenderer({ html: compiled(props) }); | ||
}, | ||
} as SectionModule, | ||
}, { | ||
...currentSourceMap, | ||
[sectionName]: () => { | ||
return Promise.resolve({ | ||
functionRef: sectionName, | ||
inputSchema: { | ||
file: import.meta.url, | ||
name: crypto.randomUUID(), | ||
type: "inline", | ||
value: { | ||
type: "object", | ||
properties, | ||
}, | ||
}, | ||
} as BlockModuleRef); | ||
}, | ||
}]; | ||
}, | ||
[{}, {}] as [Record<string, SectionModule>, SourceMap], | ||
); | ||
return { manifest: { ...manifest, sections } as Manifest, state, sourceMap }; | ||
} | ||
|
||
export type AppContext = AC<ReturnType<typeof App>>; |
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,32 @@ | ||
import { join } from "std/path/mod.ts"; | ||
|
||
const appName = Deno.args[0]; | ||
const decoTsPath = join(Deno.cwd(), "deco.ts"); | ||
const decoTs = await Deno.readTextFile(decoTsPath); | ||
|
||
await Deno.mkdir(join(Deno.cwd(), appName)); | ||
await Deno.writeTextFile( | ||
decoTsPath, | ||
decoTs.replace(` apps: [`, ` apps: [\n app("${appName}"),`), | ||
); | ||
|
||
await Deno.writeTextFile( | ||
join(Deno.cwd(), appName, "mod.ts"), | ||
` | ||
import type { App, AppContext as AC } from "$live/mod.ts"; | ||
import manifest, { Manifest } from "./manifest.gen.ts"; | ||
// deno-lint-ignore ban-types | ||
export type State = {}; | ||
/** | ||
* @title ${appName} | ||
*/ | ||
export default function App( | ||
state: State, | ||
): App<Manifest, State> { | ||
return { manifest, state }; | ||
} | ||
export type AppContext = AC<ReturnType<typeof App>>; | ||
`, | ||
); |