Skip to content

Commit

Permalink
Anchor to hash (#838)
Browse files Browse the repository at this point in the history
* 🚧 UPDATE: let's move hash to hash!

* using param "hash" & update tests

* 🚧 UPDATE: changelogs

* format
  • Loading branch information
jycouet authored Jan 19, 2025
1 parent 20c6346 commit 2b253c0
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-lies-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': patch
---

update stats link to bluesky
5 changes: 5 additions & 0 deletions .changeset/rich-berries-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': patch
---

add hash option to path (to manage anchor of a page)
7 changes: 4 additions & 3 deletions packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ const PAGES = {
return `/a/${params['rest']?.join('/')}/z`
},
'/anchors': (params: {
anchor: 'section0' | 'section1' | 'section2' | 'section3'
hash: 'section0' | 'section1' | 'section2' | 'section3'
anotherOne?: string
}) => {
return `/anchors${appendSp({ __KIT_ROUTES_ANCHOR__: params['anchor'], anotherOne: params['anotherOne'] })}`
params['hash'] = params['hash'] ?? 'section0'
return `/anchors${appendSp({ anotherOne: params['anotherOne'], __KIT_ROUTES_ANCHOR__: params['hash'] })}`
},
'/lay/normal': `/lay/normal`,
'/lay/root-layout': `/lay/root-layout`,
Expand Down Expand Up @@ -335,7 +336,7 @@ export type KIT_ROUTES = {
siteId: never
contractId: never
rest: never
anchor: never
hash: never
anotherOne: never
ids: never
locale: never
Expand Down
85 changes: 57 additions & 28 deletions packages/vite-plugin-kit-routes/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export type CustomPath<Params extends string | never = string> = {
* }
*/
explicit_search_params?: Record<string, ExplicitSearchParam>

/**
* Specify for this route the type & a default.
* @example
Expand All @@ -252,12 +253,27 @@ export type CustomPath<Params extends string | never = string> = {
* }
*/
params?: Partial<Record<Params, ExtendParam>>

/**
* If `with`, you can add extra search params to this route (without any typecheck!)
*
* ⚠️ **We don't recommend to use it, but it can be useful in some cases.**
*/
extra_search_params?: 'default' | 'with' | 'without'

/**
* Specify the hash of the route (also named anchor sometimes).
* @example
* hash: {
* type: '"section0" | "section1" | "section2" | "section3"',
* required: true,
* default: '"section0"',
* }
*/
hash?: ExtendParam & {
required?: boolean
default?: string
}
}

export type OverrideParam = {
Expand Down Expand Up @@ -288,23 +304,6 @@ export type ExplicitSearchParam = ExtendParam & {
* @default 'split'
*/
arrayMode?: 'join' | 'split'

/**
* If `true`, the key has no impact on the route.
* and one of the type will be used as the #hash.
* @example
* explicit_search_params: {
* anchor: {
* type: '"section1" | "section2" | "section3"',
* required: true,
* isAnchor: true
* }
* }
* will result in something like : /anchors#section1
*
* @default false
*/
isAnchor?: boolean
}

export const log = new Log('Kit Routes')
Expand Down Expand Up @@ -598,6 +597,19 @@ export function buildMetadata(
let isAllOptional = paramsFromPath.filter((c) => !c.optional).length === 0
const paramsReq = paramsFromPath.filter((c) => !c.optional)

if (customConf.hash) {
customConf.explicit_search_params = {
...customConf.explicit_search_params,
hash: {
type: customConf.hash.type,
required: customConf.hash.required,
default: customConf.hash.default,
// @ts-expect-error
isAnchor: true,
},
}
}

// custom search Param?
const explicit_search_params_to_function: [param: string, val: string][] = []
if (customConf.explicit_search_params) {
Expand All @@ -610,6 +622,7 @@ export function buildMetadata(
type: sp[1].type,
default: sp[1].default,
isArray: false,
// @ts-expect-error
isAnchor: sp[1].isAnchor,
}

Expand All @@ -636,6 +649,7 @@ export function buildMetadata(
const val = paramsIsOptional ? `params?.['${sp[0]}']` : `params['${sp[0]}']`

let key = sp[0]
// @ts-expect-error
if (sp[1].isAnchor) {
key = `__KIT_ROUTES_ANCHOR__`
}
Expand Down Expand Up @@ -1121,21 +1135,36 @@ function theEnd(

log.success(`${green('Stats:')} ${stats.join(' | ')}`)
log.info(
`${gray(' Share on TwiX:')} ${cyan(
`https://twitter.com/intent/tweet?text=` +
`${encodeURI(`🚀 Check out my `)}%23${encodeURI(
`KitRoutes stats 🚀\n\n` +
`- Routes: ${nbRoutes} (${objTypes.map((c) => c.files.length).join(', ')})\n` +
`- Points: ${confgPoints}\n` +
`- Score: ${score}\n` +
`- Format: "${options?.format}${shortV}"\n` +
`- Version: ${version}\n\n` +
`👀 @jycouet`,
)}`,
`${gray(' Share on bluesky:')} ${cyan(
createBSkyIntent([
`🚀 Check out my #KitRoutes stats 🚀`,
'',
`- Routes: ${nbRoutes} (${objTypes.map((c) => c.files.length).join(', ')})`,
`- Points: ${confgPoints}`,
`- Score: ${score}`,
`- Format: "${options?.format}${shortV}"`,
`- Version: ${version}`,
'',
`@jyc.dev 👀`,
]),
)}`,
)
}
}
// TODO: fix this one day!
// https://github.com/bluesky-social/social-app/issues/6133
export function createBSkyIntent(msg: string[]) {
// const lowerCaseUserAgent = navigator.userAgent.toLowerCase()

// let lineBreak = '\r\n'

// if (lowerCaseUserAgent.includes('windows')) {
// }
const lineBreak = '<br />'
// console.log(`lowerCaseUserAgent`, { lowerCaseUserAgent, lineBreak })

return `https://bsky.app/intent/compose?text=${encodeURIComponent(msg.join(lineBreak))}`
}

/**
* First you can start with something simple:
Expand Down
9 changes: 4 additions & 5 deletions packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,14 @@ describe('run()', async () => {
},
anchors: {
explicit_search_params: {
anchor: {
type: '"section0" | "section1" | "section2" | "section3"',
required: true,
isAnchor: true,
},
// anotherOne: {
// type: 'string',
// },
},
hash: {
type: '"section0" | "section1" | "section2" | "section3"',
required: true,
},
},
},
SERVERS: {},
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-kit-routes/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<a href="/COUCOU-Yop">Unsafe Link</a>
</li>
<li>
<a href={route('/anchors', { anchor: 'section0', anotherOne: 'coucou' })}>Anchors</a>
<a href={route('/anchors', { hash: 'section0', anotherOne: 'coucou' })}>Anchors</a>
</li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"/a/[...rest]/z": (params: { rest: (string | number)[] }) => {
return `/a/${params['rest']?.join('/')}/z`
},
"/anchors": (params: { anchor: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['anchor'] })}`
"/anchors": (params: { hash: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['hash'] })}`
},
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
Expand Down Expand Up @@ -202,5 +202,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET /server_func_get': never, 'GET /server_func_get_and_': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'id' | 'lang', 'create /site': 'lang', 'update /site/[id]': 'id' | 'lang', 'delete /site/[id]': 'id' | 'lang', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"/a/[...rest]/z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
},
"/anchors": (anchor: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': anchor })}`
"/anchors": (hash: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': hash })}`
},
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
Expand Down Expand Up @@ -203,5 +203,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET /server_func_get': never, 'GET /server_func_get_and_': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'lang' | 'id', 'create /site': 'lang', 'update /site/[id]': 'lang' | 'id', 'delete /site/[id]': 'lang' | 'id', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"a_rest_z": (params: { rest: (string | number)[] }) => {
return `/a/${params['rest']?.join('/')}/z`
},
"anchors": (params: { anchor: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['anchor'] })}`
"anchors": (params: { hash: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['hash'] })}`
},
"lay_normal": `/lay/normal`,
"lay_root_layout": `/lay/root-layout`,
Expand Down Expand Up @@ -206,5 +206,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET_server_func_get': never, 'GET_server_func_get_and': never, 'POST_server_func_post': never, 'GET_contract': 'lang', 'POST_contract': 'lang', 'GET_site': 'lang', 'GET_api_graphql': never, 'POST_api_graphql': never, 'GET_data_errors_locale_json': 'locale' }
ACTIONS: { 'default_contract_id': 'id' | 'lang', 'create_site': 'lang', 'update_site_id': 'id' | 'lang', 'delete_site_id': 'id' | 'lang', 'noSatisfies_site_contract': 'lang', 'send_site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'ids': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'ids': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"a_rest_z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
},
"anchors": (anchor: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': anchor })}`
"anchors": (hash: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': hash })}`
},
"lay_normal": `/lay/normal`,
"lay_root_layout": `/lay/root-layout`,
Expand Down Expand Up @@ -207,5 +207,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET_server_func_get': never, 'GET_server_func_get_and': never, 'POST_server_func_post': never, 'GET_contract': 'lang', 'POST_contract': 'lang', 'GET_site': 'lang', 'GET_api_graphql': never, 'POST_api_graphql': never, 'GET_data_errors_locale_json': 'locale' }
ACTIONS: { 'default_contract_id': 'lang' | 'id', 'create_site': 'lang', 'update_site_id': 'lang' | 'id', 'delete_site_id': 'lang' | 'id', 'noSatisfies_site_contract': 'lang', 'send_site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'ids': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'ids': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"/a/[...rest]/z": (params: { rest: (string | number)[] }) => {
return `/a/${params['rest']?.join('/')}/z`
},
"/anchors": (params: { anchor: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['anchor'] })}`
"/anchors": (params: { hash: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['hash'] })}`
},
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
Expand Down Expand Up @@ -234,5 +234,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET /server_func_get': never, 'GET /server_func_get_and_': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'id' | 'lang', 'create /site': 'lang', 'update /site/[id]': 'id' | 'lang', 'delete /site/[id]': 'id' | 'lang', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const PAGES = {
"/a/[...rest]/z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
},
"/anchors": (anchor: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': anchor })}`
"/anchors": (hash: ("section0" | "section1" | "section2" | "section3"), params?: { }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': hash })}`
},
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
Expand Down Expand Up @@ -235,5 +235,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET /server_func_get': never, 'GET /server_func_get_and_': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'lang' | 'id', 'create /site': 'lang', 'update /site/[id]': 'lang' | 'id', 'delete /site/[id]': 'lang' | 'id', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const PAGES = {
"/a/[...rest]/z": (params: { rest: (string | number)[] }) => {
return `/a/${params['rest']?.join('/')}/z`
},
"/anchors": (params: { anchor: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['anchor'] })}`
"/anchors": (params: { hash: ("section0" | "section1" | "section2" | "section3") }) => {
return `/anchors${appendSp({ '__KIT_ROUTES_ANCHOR__': params['hash'] })}`
},
"/lay/(layVerySpecial)/normal": `/lay/normal`,
"/lay/(layVerySpecial)/root-layout": `/lay/root-layout`,
Expand Down Expand Up @@ -234,5 +234,5 @@ export type KIT_ROUTES = {
SERVERS: { 'GET /(servers)/server_func_get': never, 'GET /(servers)/server_func_get_and_': never, 'POST /(servers)/server_func_post': never, 'GET /[[lang]]/contract': 'lang', 'POST /[[lang]]/contract': 'lang', 'GET /[[lang]]/site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /[[lang]]/contract/[id]': 'id' | 'lang', 'create /[[lang]]/site': 'lang', 'update /[[lang]]/site/[id]': 'id' | 'lang', 'delete /[[lang]]/site/[id]': 'id' | 'lang', 'noSatisfies /[[lang]]/site_contract': 'lang', 'send /[[lang]]/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'anchor': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
Params: { 'first': never, 'lang': never, 'id': never, 'limit': never, 'demo': never, 'da-sh': never, 'siteId': never, 'contractId': never, 'rest': never, 'hash': never, 'locale': never, 'extra': never, 'name': never, 'str': never, 's': never, 'd': never }
}
Loading

0 comments on commit 2b253c0

Please sign in to comment.