Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add document #123

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/init-node/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ runs:
- uses: actions/setup-node@v4
with:
cache: yarn
node-version: 18
node-version: 20

- name: cache node_modules
id: cache_node_modules
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/document.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: Deploy Document to GitHub Pages

on:
push:
tags:
- "**"
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install, build, and upload your site
uses: withastro/action@v1
with:
path: ./docs
node-version: 20

deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
publish: yarn run changeset publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
"files": {
"ignore": ["node_modules", "coverage"]
"ignore": ["node_modules", "coverage", ".astro", ".yarn"]
},
"organizeImports": {
"enabled": true
Expand Down
214 changes: 214 additions & 0 deletions docs/.astro/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
declare module 'astro:content' {
interface Render {
'.mdx': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content']
headings: import('astro').MarkdownHeading[]
remarkPluginFrontmatter: Record<string, any>
}>
}
}

declare module 'astro:content' {
interface Render {
'.md': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content']
headings: import('astro').MarkdownHeading[]
remarkPluginFrontmatter: Record<string, any>
}>
}
}

declare module 'astro:content' {
export { z } from 'astro/zod'

type Flatten<T> = T extends { [K: string]: infer U } ? U : never

export type CollectionKey = keyof AnyEntryMap
export type CollectionEntry<C extends CollectionKey> = Flatten<AnyEntryMap[C]>

export type ContentCollectionKey = keyof ContentEntryMap
export type DataCollectionKey = keyof DataEntryMap

// This needs to be in sync with ImageMetadata
export type ImageFunction = () => import('astro/zod').ZodObject<{
src: import('astro/zod').ZodString
width: import('astro/zod').ZodNumber
height: import('astro/zod').ZodNumber
format: import('astro/zod').ZodUnion<
[
import('astro/zod').ZodLiteral<'png'>,
import('astro/zod').ZodLiteral<'jpg'>,
import('astro/zod').ZodLiteral<'jpeg'>,
import('astro/zod').ZodLiteral<'tiff'>,
import('astro/zod').ZodLiteral<'webp'>,
import('astro/zod').ZodLiteral<'gif'>,
import('astro/zod').ZodLiteral<'svg'>,
import('astro/zod').ZodLiteral<'avif'>,
]
>
}>

type BaseSchemaWithoutEffects =
| import('astro/zod').AnyZodObject
| import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]>
| import('astro/zod').ZodDiscriminatedUnion<string, import('astro/zod').AnyZodObject[]>
| import('astro/zod').ZodIntersection<BaseSchemaWithoutEffects, BaseSchemaWithoutEffects>

type BaseSchema = BaseSchemaWithoutEffects | import('astro/zod').ZodEffects<BaseSchemaWithoutEffects>

export type SchemaContext = { image: ImageFunction }

type DataCollectionConfig<S extends BaseSchema> = {
type: 'data'
schema?: S | ((context: SchemaContext) => S)
}

type ContentCollectionConfig<S extends BaseSchema> = {
type?: 'content'
schema?: S | ((context: SchemaContext) => S)
}

type CollectionConfig<S> = ContentCollectionConfig<S> | DataCollectionConfig<S>

export function defineCollection<S extends BaseSchema>(input: CollectionConfig<S>): CollectionConfig<S>

type AllValuesOf<T> = T extends any ? T[keyof T] : never
type ValidContentEntrySlug<C extends keyof ContentEntryMap> = AllValuesOf<ContentEntryMap[C]>['slug']

export function getEntryBySlug<C extends keyof ContentEntryMap, E extends ValidContentEntrySlug<C> | (string & {})>(
collection: C,
// Note that this has to accept a regular string too, for SSR
entrySlug: E,
): E extends ValidContentEntrySlug<C> ? Promise<CollectionEntry<C>> : Promise<CollectionEntry<C> | undefined>

export function getDataEntryById<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C]>(
collection: C,
entryId: E,
): Promise<CollectionEntry<C>>

export function getCollection<C extends keyof AnyEntryMap, E extends CollectionEntry<C>>(
collection: C,
filter?: (entry: CollectionEntry<C>) => entry is E,
): Promise<E[]>
export function getCollection<C extends keyof AnyEntryMap>(
collection: C,
filter?: (entry: CollectionEntry<C>) => unknown,
): Promise<CollectionEntry<C>[]>

export function getEntry<C extends keyof ContentEntryMap, E extends ValidContentEntrySlug<C> | (string & {})>(entry: {
collection: C
slug: E
}): E extends ValidContentEntrySlug<C> ? Promise<CollectionEntry<C>> : Promise<CollectionEntry<C> | undefined>
export function getEntry<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C] | (string & {})>(entry: {
collection: C
id: E
}): E extends keyof DataEntryMap[C] ? Promise<DataEntryMap[C][E]> : Promise<CollectionEntry<C> | undefined>
export function getEntry<C extends keyof ContentEntryMap, E extends ValidContentEntrySlug<C> | (string & {})>(
collection: C,
slug: E,
): E extends ValidContentEntrySlug<C> ? Promise<CollectionEntry<C>> : Promise<CollectionEntry<C> | undefined>
export function getEntry<C extends keyof DataEntryMap, E extends keyof DataEntryMap[C] | (string & {})>(
collection: C,
id: E,
): E extends keyof DataEntryMap[C] ? Promise<DataEntryMap[C][E]> : Promise<CollectionEntry<C> | undefined>

/** Resolve an array of entry references from the same collection */
export function getEntries<C extends keyof ContentEntryMap>(
entries: {
collection: C
slug: ValidContentEntrySlug<C>
}[],
): Promise<CollectionEntry<C>[]>
export function getEntries<C extends keyof DataEntryMap>(
entries: {
collection: C
id: keyof DataEntryMap[C]
}[],
): Promise<CollectionEntry<C>[]>

export function reference<C extends keyof AnyEntryMap>(
collection: C,
): import('astro/zod').ZodEffects<
import('astro/zod').ZodString,
C extends keyof ContentEntryMap
? {
collection: C
slug: ValidContentEntrySlug<C>
}
: {
collection: C
id: keyof DataEntryMap[C]
}
>
// Allow generic `string` to avoid excessive type errors in the config
// if `dev` is not running to update as you edit.
// Invalid collection names will be caught at build time.
export function reference<C extends string>(
collection: C,
): import('astro/zod').ZodEffects<import('astro/zod').ZodString, never>

type ReturnTypeOrOriginal<T> = T extends (...args: any[]) => infer R ? R : T
type InferEntrySchema<C extends keyof AnyEntryMap> = import('astro/zod').infer<
ReturnTypeOrOriginal<Required<ContentConfig['collections'][C]>['schema']>
>

type ContentEntryMap = {
docs: {
'api/fold.md': {
id: 'api/fold.md'
slug: 'api/fold'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'api/omit.md': {
id: 'api/omit.md'
slug: 'api/omit'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'api/pick.md': {
id: 'api/pick.md'
slug: 'api/pick'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'api/twist.md': {
id: 'api/twist.md'
slug: 'api/twist'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'api/unfold.md': {
id: 'api/unfold.md'
slug: 'api/unfold'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'guides/start.md': {
id: 'guides/start.md'
slug: 'guides/start'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.md'] }
'index.mdx': {
id: 'index.mdx'
slug: 'index'
body: string
collection: 'docs'
data: InferEntrySchema<'docs'>
} & { render(): Render['.mdx'] }
}
}

type DataEntryMap = {}

type AnyEntryMap = ContentEntryMap & DataEntryMap

type ContentConfig = typeof import('../src/content/config.js')
}
35 changes: 35 additions & 0 deletions docs/astro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'

// biome-ignore lint/style/noDefaultExport: <explanation>
export default defineConfig({
site: 'https://hacomono-lib.github.io',
base: '/json-origami',
integrations: [
starlight({
title: 'json-origami',
social: {
github: 'https://github.com/hacomono-lib/json-origami',
},
sidebar: [
{
label: 'Home',
link: '/',
},
{
label: 'Introduction',
collapsed: false,
items: [
{ label: 'Getting Started', link: '/guides/start' },
{ label: 'Concepts', link: '/guides/concepts' },
],
},
{
label: 'Api',
collapsed: false,
autogenerate: { directory: '/api', collapsed: false },
},
],
}),
],
})
1 change: 1 addition & 0 deletions docs/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/src/assets/houston.webp
Binary file not shown.
7 changes: 7 additions & 0 deletions docs/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema'
import { defineCollection } from 'astro:content'

export const collections = {

Check failure on line 4 in docs/src/content/config.ts

View workflow job for this annotation

GitHub Actions / Build

The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
docs: defineCollection({ schema: docsSchema() }),
i18n: defineCollection({ type: 'data', schema: i18nSchema() }),
}
76 changes: 76 additions & 0 deletions docs/src/content/docs/api/fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: fold
description: fold function converts a JSON object into key-value pair object by folding it.
---

`fold` function converts a JSON object into key-value pair object by folding it.

```ts
fold(object, option?)
```

## Arguments

- ### `object`

type: `object`

The JSON object to fold.

e.g.
`{ a: { b: "c" } }` will be folded to `{ "a.b": "c" }`.

- ### `option`

type: `object`

- #### `arrayIndex`

type: `"dot" | "bracket"`

default: `"bracket"`

The index of array to fold.

e.g.
`{ a: ["b"] }` will be folded to `{ "a[0]": "b" }` if `arrayIndex` is `"bracket"`,
and will be folded to `{ "a.0": "b" }` if `arrayIndex` is `"dot"`.

- #### `keyPrefix`

type: `string`

default: `""`

The prefix to prepend to the folded key.

### Example

```ts
import { fold } from 'json-origami';

const user = {
id: 1,
name: 'John',
age: 20,
address: {
street: '123 Main St',
city: 'New York',
state: 'NY',
zip: '10001',
},
};

const folded = fold(user)
/**
* {
* id: 1,
* name: 'John',
* age: 20,
* 'address.street': '123 Main St',
* 'address.city': 'New York',
* 'address.state': 'NY',
* 'address.zip': '10001',
* }
*/
```
Loading
Loading