Skip to content

Commit

Permalink
mv packages -> src
Browse files Browse the repository at this point in the history
  • Loading branch information
jldec committed Sep 17, 2024
1 parent 8670937 commit 45839c4
Show file tree
Hide file tree
Showing 33 changed files with 55 additions and 16 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"scripts": {},
"devDependencies": {},
"prettier": {
"useTabs": true,
"singleQuote": true,
Expand Down
2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# https://pnpm.io/pnpm-workspace_yaml
packages:
- 'packages/*'
- 'src/*'
4 changes: 2 additions & 2 deletions public/content/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ title: Progress

1. Added [AI generated summaries](https://presskit.jldec.me/summarize) while participating in [Cozy Hack SF](https://lu.ma/wco3g23k?tk=5aQXWb). This feature has been been removed for now.

1. Used [daisyUI](https://presskit.jldec.me/daisyui) for responsive [navbar/navbar](https://daisyui.com/components/drawer/#navbar-menu-for-desktop--sidebar-drawer-for-mobile) navigation.
1. Used [daisyUI](https://presskit.jldec.me/daisyui) for responsive [navbar/navbar](https://daisyui.com/components/drawer/#navbar-menu-for-desktop--sidebar-drawer-for-mobile) navigation.

1. Auto-migrate images to R2.

A plugin to the markdown parser [rewrites](/packages/worker/src/markdown/rewrite-url.ts) image urls to point to `/img/<hash>?og=<original-url>`.
A plugin to the markdown parser [rewrites image urls](/src/worker/src/markdown/image-plugin.ts) to point to `/img/<hash>?og=<original-url>`.

The /img/* handler tries to fetch from R2 first, and falls back to using the og url and then uploading to R2.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions packages/worker/package.json → src/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"partyserver": "^0.0.55"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240821.0",
"@cloudflare/workers-types": "^4.20240909.0",
"@types/markdown-it": "^14.1.2",
"htmx.org": "^2.0.1",
"vitest": "^2.0.5",
"wrangler": "^3.72.2"
"vitest": "^2.1.1",
"wrangler": "^3.78.2"
}
}
6 changes: 2 additions & 4 deletions packages/worker/src/api.ts → src/worker/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hono } from './types'
import { getTree } from './markdown/get-tree'

// @ts-expect-error
import manifest from '__STATIC_CONTENT_MANIFEST'
Expand Down Expand Up @@ -73,8 +74,5 @@ api.delete('/images', async (c) => {
})

api.get('/tree', async (c) => {
let cachedTree = await c.env.PAGE_CACHE.get('TREE')
if (cachedTree !== null) {
return fjson(JSON.parse(cachedTree))
} else return c.notFound()
return fjson(await getTree('/', c))
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { type FC } from 'hono/jsx'
export const AdminLayout: FC = ({ page }) => (
<>
{raw(page?.html ?? '')}
<button hx-get="/api/tree" hx-target=".json">
tree
</button>{' '}
<button hx-get="/api/echo" hx-target=".json">
echo
</button>{' '}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Page, Context, StatusCode } from '../types'
import type { Page, Context } from '../types'
import { parseFrontmatter } from './parse-frontmatter'
import { parseMarkdown } from './parse-markdown'

Expand Down
32 changes: 32 additions & 0 deletions src/worker/src/markdown/get-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Context } from '../types'
// @ts-expect-error
import manifest from '__STATIC_CONTENT_MANIFEST'

// TODO: fetch tree and use to validate markdown paths
const treeUrl = 'https://api.github.com/repos/jldec/presskit/git/trees/HEAD'

export async function getTree(path: string, c: Context) {
if (c.req.header('Cache-Control') !== 'no-cache') {
const cachedContent = await c.env.TREE_CACHE.get(path)
if (cachedContent !== null) return JSON.parse(cachedContent)
}
let tree
if (c.env.ENVIRONMENT === 'dev') {
return JSON.parse(manifest)
}
let resp = await fetch(treeUrl, {
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${c.env.GH_PAT}`,
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'presskit-worker'
}
})
if (resp.ok) {
tree = await resp.json()
c.executionCtx.waitUntil(c.env.TREE_CACHE.put('TREE', JSON.stringify(tree)))
return tree
} else {
return null
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/worker/src/types.ts → src/worker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type { StatusCode } from 'hono/utils/http-status'

export type Bindings = {
PAGE_CACHE: KVNamespace
TREE_CACHE: KVNamespace
IMAGES: R2Bucket
AI: any
GH_PAT: string
Expand Down
File renamed without changes.
10 changes: 8 additions & 2 deletions packages/worker/wrangler.toml → src/worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name = "presskit-worker"
compatibility_date = "2024-07-12"
main = "src/index.tsx"
compatibility_flags = [ "nodejs_compat_v2" ]

# secrets
# GH_PAT: Expires on Mon, Sep 16 2024
Expand All @@ -19,12 +20,17 @@ binding = 'IMAGES'
bucket_name = 'presskit-img-v0'

[[kv_namespaces]]
# pnpm wrangler kv namespace create PAGE_CACHE
# pnpm wrangler kv namespace create PAGE_CACHE --preview
# pnpm wrangler kv namespace create PAGE_CACHE/TREE_CACHE
# pnpm wrangler kv namespace create PAGE_CACHE/TREE_CACHE --preview
binding = "PAGE_CACHE"
id = "65c4126075304a93baaacea9187ff8a9"
preview_id = "b52a7190959c4127b32cd717b9aa8936"

[[kv_namespaces]]
binding = "TREE_CACHE"
id = "b79c4cd1b3fd43fda786a45225650df2"
preview_id = "daafea7aa1c748f39e1951085224ea4c"

[[durable_objects.bindings]]
name = "Chat"
class_name = "Chat"
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"skipLibCheck": true,
"lib": ["ESNext"],
},
"include": ["./packages/**/*.ts", "./packages/**/*.tsx"],
"exclude": ["./packages/**/*.test.ts"]
"include": ["./src/**/*.ts", "./src/**/*.tsx"],
"exclude": ["./src/**/*.test.ts"]
}

0 comments on commit 45839c4

Please sign in to comment.