Skip to content

Commit

Permalink
fix(schema): vscode autocompletion, simplify meta
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 14, 2022
1 parent c8adb3f commit 50a85cb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
10 changes: 5 additions & 5 deletions packages/schema/src/link.ts
@@ -1,4 +1,4 @@
import type { HttpEventAttributes } from '@zhead/schema/src/attributes'
import type { HttpEventAttributes } from './attributes'

export type LinkRelTypes = 'alternate' |
'author' |
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface Link extends HttpEventAttributes {
* The value must be a string that matches the CSS <color> production,
* defining a suggested color that user agents can use to customize the display
* of the icon that the user sees when they pin your site.
*
*
* @see https://html.spec.whatwg.org/multipage/semantics.html#attr-link-color
*/
color?: string
Expand Down Expand Up @@ -155,15 +155,15 @@ export interface Link extends HttpEventAttributes {
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-rel
*/
rel?: LinkRelTypes | string
rel?: LinkRelTypes | (string & Record<never, never>)
/**
* This attribute defines the sizes of the icons for visual media contained in the resource.
* It must be present only if the rel contains a value of icon or a non-standard type
* such as Apple's apple-touch-icon.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-sizes
*/
sizes?: 'any' | '16x16x' | '32x32' | '64x64' | '180x180' | string
sizes?: 'any' | '16x16x' | '32x32' | '64x64' | '180x180' | (string & Record<never, never>)
/**
* The title attribute has special semantics on the <link> element.
* When used on a <link rel="stylesheet"> it defines a default or an alternate stylesheet.
Expand Down Expand Up @@ -255,7 +255,7 @@ export interface Link extends HttpEventAttributes {
'video/3gpp' |
'audio/3gpp' |
'video/3gpp2' |
'audio/3gpp2' | string
'audio/3gpp2' | (string & Record<never, never>)
/**
* This attribute defines the unique ID.
*/
Expand Down
111 changes: 83 additions & 28 deletions packages/schema/src/meta.ts
@@ -1,28 +1,83 @@
import type { MetaFlat } from './meta-flat'
export type MetaNames =
'charset' |
'description' |
'color-scheme' |
'application-name' |
'author' |
'creator' |
'publisher' |
'generator' |
'referrer' |
'viewport' |
'robots' |
'google' |
'googlebot' |
'google-site-verification' |
'rating' |
'fb:app_id' |
'theme-color' |
'mobile-web-app-capable' |
'apple-mobile-web-app-capable' |
'apple-mobile-web-app-status-bar-style' |
'apple-mobile-web-app-title' |
'apple-itunes-app' |
'format-detection' |
'msapplication-TileImage' |
'msapplication-TileColor' |
'msapplication-Config' |
'content-security-policy' |
'content-type' |
'default-style' |
'x-ua-compatible' |
'refresh'

type Kebab<T extends string, A extends string = ''> =
T extends `${infer F}${infer R}` ?
Kebab<R, `${A}${F extends Lowercase<F> ? '' : '-'}${Lowercase<F>}`> :
A

type KebabColon<T extends string, A extends string = ''> =
T extends `${infer F}${infer R}` ?
KebabColon<R, `${A}${F extends Lowercase<F> ? '' : ':'}${Lowercase<F>}`> :
A

type FixCase<T extends string> =
T extends 'fbAppId' ? 'fb:app_id' :
T extends 'ogImageSecureUrl' ? 'og:image:secure_url' :
T extends 'ogVideoSecureUrl' ? 'og:video:secure_url' :
T extends
`og${infer R}` | `twitter${infer R}`
? KebabColon<T> : Kebab<T>

type ContentKeys<T extends string> = T extends
`og${infer R}` | `twitter${infer R}` ? never : T

type PropertyKeys<T extends string> = T extends
`og${infer R}` | `twitter${infer R}` ? T : never
export type MetaProperties = 'og:url' |
'og:title' |
'og:description' |
'og:type' |
'og:locale' |
'og:locale:alternate' |
'og:determiner' |
'og:site:name' |
'og:video' |
'og:video:url' |
'og:video:secure_url' |
'og:video:type' |
'og:video:width' |
'og:video:height' |
'og:image' |
'og:image:url' |
'og:image:secure_url' |
'og:image:type' |
'og:image:width' |
'og:image:height' |
'fb:app:id' |
'twitter:card' |
'twitter:title' |
'twitter:description' |
'twitter:image' |
'twitter:image:alt' |
'twitter:site' |
'twitter:site:id' |
'twitter:creator' |
'twitter:creator:id' |
'twitter:player' |
'twitter:player:width' |
'twitter:player:height' |
'twitter:player:stream' |
'twitter:app:name:iphone' |
'twitter:app:id:iphone' |
'twitter:app:url:iphone' |
'twitter:app:name:ipad' |
'twitter:app:id:ipad' |
'twitter:app:url:ipad' |
'twitter:app:name:googleplay' |
'twitter:app:id:googleplay' |
'twitter:app:url:googleplay' |
'twitter:data:1' |
'twitter:label:1' |
'twitter:data:2' |
'twitter:label:2'

export interface Meta {
/**
Expand All @@ -34,13 +89,13 @@ export interface Meta {
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#attr-charset
*/
charset?: string
charset?: 'utf-8' | (string & Record<never, never>)
/**
* This attribute contains the value for the http-equiv or name attribute, depending on which is used.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#attr-content
*/
content?: (MetaFlat[keyof MetaFlat] extends string ? MetaFlat[keyof MetaFlat] : never) | string | number
content?: string
/**
* Defines a pragma directive. The attribute is named http-equiv(alent) because all the allowed values are names of
* particular HTTP headers.
Expand All @@ -59,13 +114,13 @@ export interface Meta {
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta#attr-name
*/
name?: FixCase<ContentKeys<keyof MetaFlat>> | string
name?: MetaNames | (string & Record<never, never>)
/**
* The property attribute is used to define a property associated with the content attribute.
*
* Mainly used for og and twitter meta tags.
*/
property?: FixCase<PropertyKeys<keyof MetaFlat>> | string
property?: MetaProperties | (string & Record<never, never>)
/**
* This attribute defines the unique ID.
*/
Expand Down

0 comments on commit 50a85cb

Please sign in to comment.