Skip to content

Commit 08a39dc

Browse files
committed
feat: add qr code to startup urls if --host option when
1 parent 42409a4 commit 08a39dc

File tree

7 files changed

+66
-83
lines changed

7 files changed

+66
-83
lines changed

packages/kitbook/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"dist"
6262
],
6363
"scripts": {
64-
"dev": "vite dev",
64+
"dev": "vite dev --host",
6565
"build": "vite build",
6666
"build:ci": "cd ../.. && pnpm build:deps && cd packages/kitbook && pnpm build",
6767
"preview": "vite preview",
@@ -97,6 +97,7 @@
9797
"birpc": "^0.2.17",
9898
"codemirror": "^6.0.1",
9999
"fast-glob": "^3.3.2",
100+
"qrcode-terminal": "^0.12.0",
100101
"rehype-autolink-headings": "^6.1.1",
101102
"rehype-format": "^4.0.1",
102103
"rehype-slug": "^5.1.0",
@@ -122,6 +123,7 @@
122123
"@sveltejs/vite-plugin-svelte": "^3.1.0",
123124
"@types/mdast": "^4.0.2",
124125
"@types/node": "^20.11.30",
126+
"@types/qrcode-terminal": "^0.12.2",
125127
"@unocss/svelte-scoped": "^0.59.4",
126128
"prettier": "^2.8.8",
127129
"publint": "^0.2.7",
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createRawSnippet, hydrate } from 'svelte'
2+
import { render } from 'svelte/server'
3+
import { browser } from '$app/environment'
4+
5+
export function as_snippet(component: Component) {
6+
return createRawSnippet((props_function) => {
7+
const props = props_function ? props_function() : {}
8+
return {
9+
render: () => `
10+
<div>${browser ? '' : render(component, { props }).body}</div>
11+
`,
12+
setup(target) {
13+
hydrate(component, { target, props })
14+
},
15+
}
16+
})
17+
}

packages/kitbook/src/lib/lz/lz-string.ts

+1-72
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
//
99
// LZ-based compression algorithm, version 1.4.4
1010

11-
// If other compressions are needed, uncomment and add tests from https://github.com/pieroxy/lz-string/blob/master/tests/lz-string-spec.js
11+
// If other compressions are needed pull from https://github.com/pieroxy/lz-string (bring tests across also)
1212

1313
// Note also the esm port of lz-string for utf-8 only by @immutabl3/lz-string
1414

1515
const f = String.fromCharCode
16-
// const keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1716
const keyStrUriSafe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$'
1817
const baseReverseDic = {}
1918

@@ -45,70 +44,6 @@ function getBaseValue(alphabet: string, character: string) {
4544
return baseReverseDic[alphabet][character]
4645
}
4746

48-
// export function compressToBase64(input) {
49-
// if (input == null) return "";
50-
// var res = _compress(input, 6, function (a) { return keyStrBase64.charAt(a); });
51-
// switch (res.length % 4) { // To produce valid Base64
52-
// default: // When could this happen ?
53-
// case 0: return res;
54-
// case 1: return res + "===";
55-
// case 2: return res + "==";
56-
// case 3: return res + "=";
57-
// }
58-
// }
59-
60-
// export function decompressFromBase64(input) {
61-
// if (input == null) return "";
62-
// if (input == "") return null;
63-
// return _decompress(input.length, 32, function (index) { return getBaseValue(keyStrBase64, input.charAt(index)); });
64-
// }
65-
66-
// export function compressToUTF16(input) {
67-
// if (input == null) return "";
68-
// return _compress(input, 15, function (a) { return f(a + 32); }) + " ";
69-
// }
70-
71-
// export function decompressFromUTF16(compressed) {
72-
// if (compressed == null) return "";
73-
// if (compressed == "") return null;
74-
// return _decompress(compressed.length, 16384, function (index) { return compressed.charCodeAt(index) - 32; });
75-
// }
76-
77-
// compress into uint8array (UCS-2 big endian format)
78-
// export function compressToUint8Array(uncompressed) {
79-
// var compressed = compress(uncompressed);
80-
// var buf = new Uint8Array(compressed.length * 2); // 2 bytes per character
81-
82-
// for (var i = 0, TotalLen = compressed.length; i < TotalLen; i++) {
83-
// var current_value = compressed.charCodeAt(i);
84-
// buf[i * 2] = current_value >>> 8;
85-
// buf[i * 2 + 1] = current_value % 256;
86-
// }
87-
// return buf;
88-
// }
89-
90-
// decompress from uint8array (UCS-2 big endian format)
91-
// export function decompressFromUint8Array(compressed) {
92-
// if (compressed === null || compressed === undefined) {
93-
// return decompress(compressed);
94-
// } else {
95-
// var buf = new Array(compressed.length / 2); // 2 bytes per character
96-
// for (var i = 0, TotalLen = buf.length; i < TotalLen; i++) {
97-
// buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1];
98-
// }
99-
100-
// var result = [];
101-
// buf.forEach(function (c) {
102-
// result.push(f(c));
103-
// });
104-
// return decompress(result.join(''));
105-
// }
106-
// }
107-
108-
// function compress(uncompressed) {
109-
// return _compress(uncompressed, 16, function (a) { return f(a); });
110-
// }
111-
11247
function _compress(uncompressed: string, bitsPerChar: number, getCharFromInt: (index: number) => any) {
11348
if (uncompressed == null)
11449
return ''
@@ -341,12 +276,6 @@ function _compress(uncompressed: string, bitsPerChar: number, getCharFromInt: (i
341276
return context_data.join('')
342277
}
343278

344-
// function decompress(compressed) {
345-
// if (compressed == null) return "";
346-
// if (compressed == "") return null;
347-
// return _decompress(compressed.length, 32768, function (index) { return compressed.charCodeAt(index); });
348-
// }
349-
350279
function _decompress(length: number, resetValue: number, getNextValue: (index: number) => any) {
351280
const dictionary = []
352281
const result = []

packages/kitbook/src/lib/open/openFiles.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getFilenameAndExtension } from './get-filename-and-extension'
33
import { serializeIntersection } from './serialize'
44

55
export function openComponent(filepath: string) {
6-
ensureFileExists(filepath)
6+
rpc_client.functions.open_or_create_file({ filepath, template: '' })
77
}
88

99
export function openVariants(filepath: string, componentDetail?: SvelteComponentDetail) {
@@ -22,7 +22,7 @@ export function sendOpenVariantsRequest(filepath: string, serializedState: Recor
2222

2323
export function openMarkdown(filepath: string) {
2424
const markdownTemplate = 'You can write some documentation for your component here using markdown.'
25-
ensureFileExists(filepath, markdownTemplate)
25+
ensure_non_svelte_file_exists(filepath, markdownTemplate)
2626
}
2727

2828
export function openComposition({ filepath, compositionName }: { filepath: string, compositionName?: string }) {
@@ -63,10 +63,10 @@ Place your Svelte composition here.
6363

6464
const compositionExtension = (compositionName && compositionName !== 'default') ? `${compositionName}.composition` : 'composition'
6565
const template = extension === 'md' ? markdownCompositionTemplate : svelteCompositionTemplate
66-
ensureFileExists(`${filepathWithoutExtension}.${compositionExtension}`, template)
66+
ensure_non_svelte_file_exists(`${filepathWithoutExtension}.${compositionExtension}`, template)
6767
}
6868

69-
function ensureFileExists(filepath: string, template = '') {
69+
function ensure_non_svelte_file_exists(filepath: string, template: string) {
7070
const pageProofPath = filepath
7171
.replace('+page', '_page')
7272
.replace('+layout', '_layout')

packages/kitbook/src/lib/plugins/main/plugin.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Plugin } from 'vite'
2+
import qr from 'qrcode-terminal'
23
import type { KitbookSettings } from '../../kitbook-types'
34
import { serializeSettings } from '../../open/serialize.js'
45
import { bold, green, reset } from '../utils/colors.js'
@@ -41,13 +42,28 @@ export function MainPlugin(settings: KitbookSettings): Plugin {
4142

4243
configureServer(server) {
4344
const { kitbookRoute, addLanguageToUrl, languages } = settings
44-
if (kitbookRoute) {
45-
const languageAwareRoute = addLanguageToUrl ? addLanguageToUrl({ code: languages[0].code, url: kitbookRoute }) : kitbookRoute
46-
const originalPrint = server.printUrls
47-
server.printUrls = () => {
48-
originalPrint()
49-
console.info(` ${green}${reset} ${bold}Kitbook${reset}: ${green}${server.config.server.https ? 'https' : 'http'}://localhost:${bold}${server.config.server.port}${reset}${green}${languageAwareRoute}${reset}`)
45+
46+
const originalPrint = server.printUrls
47+
server.printUrls = () => {
48+
originalPrint()
49+
50+
const additional_logs: string [] = []
51+
52+
if (kitbookRoute) {
53+
const languageAwareRoute = addLanguageToUrl ? addLanguageToUrl({ code: languages[0].code, url: kitbookRoute }) : kitbookRoute
54+
additional_logs.push(` ${green}${reset} ${bold}Kitbook${reset}: ${green}${server.config.server.https ? 'https' : 'http'}://localhost:${bold}${server.config.server.port}${reset}${green}${languageAwareRoute}${reset}`)
5055
}
56+
57+
const network_urls = server.resolvedUrls?.network || []
58+
if (network_urls.length) {
59+
const last_url = network_urls[network_urls.length - 1]
60+
qr.generate(last_url, { small: true }, (result) => {
61+
additional_logs.push(`\n ${green}${last_url}${reset}\n ${result.replace(/\n/g, '\n ')}`)
62+
})
63+
}
64+
65+
for (const message of additional_logs)
66+
console.info(message)
5167
}
5268
},
5369
}

packages/kitbook/src/lib/routes/[...file]/MainPage.svelte

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
{#if !variantsModule}
135135
<Button onclick={() => openVariants(`${pathWithoutExtension}.svelte`)} size="sm" form="menu"><span class="i-system-uicons-versions align--4px text-xl" /> Add Variant</Button>
136136
{/if}
137+
138+
<Button size="sm" form="menu" href="/en/zh-TW/bible">Go to sample page</Button>
137139
{/if}
138140
</div>
139141

pnpm-lock.yaml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)