Skip to content

Commit

Permalink
docs: enable twoslash
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 14, 2024
1 parent c2a9309 commit f643eef
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 87 deletions.
6 changes: 6 additions & 0 deletions docs/.vitepress/config.ts
@@ -1,5 +1,6 @@
import { defineConfig } from 'vitepress'
import type { DefaultTheme } from 'vitepress/types'
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
import { version } from '../../package.json'

const ogUrl = 'https://unocss.dev/'
Expand Down Expand Up @@ -269,6 +270,11 @@ export default defineConfig({
light: 'vitesse-light',
dark: 'vitesse-dark',
},
codeTransformers: [
transformerTwoslash({
processHoverInfo: info => info.replace(/_unocss_core\./g, ''),
}),
],
},

themeConfig: {
Expand Down
7 changes: 6 additions & 1 deletion docs/.vitepress/theme/index.ts
@@ -1,8 +1,11 @@
// https://vitepress.dev/guide/custom-theme
import { h, watch } from 'vue'
import Theme from 'vitepress/theme'
import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client'
import UnoCSSLayout from './UnoCSSLayout.vue'

import '@shikijs/vitepress-twoslash/style.css'

import './rainbow.css'
import './vars.css'
import './overrides.css'
Expand All @@ -15,7 +18,9 @@ export default {
Layout: () => {
return h(UnoCSSLayout)
},
enhanceApp({ router }) {
enhanceApp({ app, router }) {
app.use(TwoslashFloatingVue)

if (typeof window === 'undefined')
return

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/config-file.md
Expand Up @@ -4,7 +4,7 @@ We **highly recommend to use a dedicated `uno.config.ts` file** to configure you

A full featured config file looks like this:

```ts
```ts twoslash
// uno.config.ts
import {
defineConfig,
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/presets.md
Expand Up @@ -12,7 +12,7 @@ Presets are the heart of UnoCSS. They let you make your own custom framework in

To set presets to your project:

```ts
```ts twoslash
// uno.config.ts
import { defineConfig, presetAttributify, presetUno } from 'unocss'

Expand All @@ -29,7 +29,7 @@ When the `presets` option is specified, the default preset will be ignored.

To disable the default preset, you can set `presets` to an empty array:

```ts
```ts twoslash
// uno.config.ts
import { defineConfig } from 'unocss'

Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Expand Up @@ -17,6 +17,6 @@
"@iconify-json/vscode-icons": "^1.1.33",
"ofetch": "^1.3.3",
"unocss": "workspace:*",
"vitepress": "1.0.0-rc.31"
"vitepress": "^1.0.0-rc.45"
}
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"@iconify-json/twemoji": "^1.1.15",
"@iconify-json/uim": "^1.1.9",
"@iconify/json": "^2.2.192",
"@shikijs/vitepress-twoslash": "^1.1.7",
"@types/connect": "^3.4.38",
"@types/css-tree": "^2.3.7",
"@types/fs-extra": "^11.0.4",
Expand Down
19 changes: 19 additions & 0 deletions packages/preset-attributify/src/index.ts
Expand Up @@ -10,6 +10,25 @@ export * from './variant'
export * from './types'
export * from './jsx'

/**
* This enables the attributify mode for other presets.
*
* @example
*
* ```html
* <button
* bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600"
* text="sm white"
* font="mono light"
* p="y-2 x-4"
* border="2 rounded blue-200"
* >
* Button
* </button>
* ```
*
* @see https://unocss.dev/presets/attributify
*/
export const presetAttributify = definePreset((options: AttributifyOptions = {}) => {
options.strict = options.strict ?? false
options.prefix = options.prefix ?? 'un-'
Expand Down
13 changes: 13 additions & 0 deletions packages/preset-icons/src/index.ts
Expand Up @@ -17,6 +17,19 @@ async function createNodeLoader() {
catch { }
}

/**
* Use any icon with Pure CSS for UnoCSS.
*
* @example
*
* ```html
* <div class="i-mdi-alarm"></div>
* <div class="i-logos-vue text-3xl"></div>
* <button class="i-carbon-sun dark:i-carbon-moon"></div>
* ```
*
* @see https://unocss.dev/presets/icons
*/
export const presetIcons = /* @__PURE__ */ createPresetIcons(async (options) => {
const {
cdn,
Expand Down
5 changes: 5 additions & 0 deletions packages/preset-mini/src/index.ts
Expand Up @@ -72,6 +72,11 @@ export interface PresetMiniOptions extends PresetOptions {
arbitraryVariants?: boolean
}

/**
* The basic preset for UnoCSS, with only the most essential utilities.
*
* @see https://unocss.dev/presets/mini
*/
export const presetMini = definePreset((options: PresetMiniOptions = {}) => {
options.dark = options.dark ?? 'class'
options.attributifyPseudo = options.attributifyPseudo ?? false
Expand Down
5 changes: 5 additions & 0 deletions packages/preset-uno/src/index.ts
Expand Up @@ -7,6 +7,11 @@ export type { Theme }

export interface PresetUnoOptions extends PresetMiniOptions {}

/**
* The defult UnoCSS preset, provide Tailwind-like utilities with some additional features.
*
* @see https://unocss.dev/presets/uno
*/
export const presetUno = definePreset((options: PresetUnoOptions = {}) => {
const wind = presetWind(options)
return {
Expand Down
6 changes: 6 additions & 0 deletions packages/preset-web-fonts/src/index.ts
Expand Up @@ -7,6 +7,12 @@ export { createGoogleCompatibleProvider as createGoogleProvider } from './provid

const userAgentWoff2 = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
const defaultFetch = async (url: string) => (await import('ofetch')).$fetch(url, { headers: { 'User-Agent': userAgentWoff2 }, retry: 3 })

/**
* Preset for using web fonts by provide just the names.
*
* @see https://unocss.dev/presets/web-fonts
*/
const presetWebFonts = definePreset(createWebFontPreset(defaultFetch))

export default presetWebFonts
5 changes: 5 additions & 0 deletions packages/preset-wind/src/index.ts
Expand Up @@ -28,6 +28,11 @@ export interface PresetWindOptions extends PresetMiniOptions {
important?: boolean | string
}

/**
* The Tailwind CSS / Windi CSS compact preset for UnoCSS.
*
* @see https://unocss.dev/presets/wind
*/
export const presetWind = definePreset((options: PresetWindOptions = {}) => {
options.important = options.important ?? false

Expand Down
6 changes: 3 additions & 3 deletions packages/shared-docs/src/search.ts
Expand Up @@ -4,7 +4,7 @@ import { watchAtMost } from '@vueuse/core'
import Fuse from 'fuse.js'
import { createAutocomplete } from '@unocss/autocomplete'
import type { Ref } from 'vue'
import { computed, reactive, toRaw } from 'vue'
import { computed, shallowReactive, toRaw } from 'vue'
import type { DocItem, GuideItem, ResultItem, RuleItem } from './types'
import { extractColors, formatCSS, sampleArray } from './utils'

Expand All @@ -19,8 +19,8 @@ export function createSearch(
{ uno, docs, guides, limit = 50 }: SearchState,
) {
const ac = createAutocomplete(uno)
const matchedMap = reactive(new Map<string, RuleItem>())
const featuresMap = reactive(new Map<string, Set<RuleItem>>())
const matchedMap = shallowReactive(new Map<string, RuleItem>())
const featuresMap = shallowReactive(new Map<string, Set<RuleItem>>())

let fuseCollection: ResultItem[] = []

Expand Down
3 changes: 1 addition & 2 deletions packages/shared-integration/src/transformers.ts
Expand Up @@ -2,7 +2,6 @@ import type { SourceCodeTransformerEnforce, UnocssPluginContext } from '@unocss/
import MagicString from 'magic-string'
import type { EncodedSourceMap } from '@ampproject/remapping'
import remapping from '@ampproject/remapping'
import type { SourceMap } from 'rollup'
import { IGNORE_COMMENT, SKIP_COMMENT_RE } from './constants'
import { hash } from './hash'

Expand Down Expand Up @@ -44,7 +43,7 @@ export async function applyTransformers(
ctx.affectedModules.add(id)
return {
code,
map: remapping(maps, () => null) as SourceMap,
map: remapping(maps, () => null) as any,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/unocss/src/index.ts
Expand Up @@ -15,6 +15,9 @@ export { default as transformerVariantGroup } from '@unocss/transformer-variant-
export { default as transformerCompileClass } from '@unocss/transformer-compile-class'
export { default as transformerAttributifyJsx } from '@unocss/transformer-attributify-jsx'

/**
* Define UnoCSS config
*/
export function defineConfig<T extends object = Theme>(config: UserConfig<T>) {
return config
}

0 comments on commit f643eef

Please sign in to comment.