File tree Expand file tree Collapse file tree 3 files changed +91
-45
lines changed Expand file tree Collapse file tree 3 files changed +91
-45
lines changed Original file line number Diff line number Diff line change 1+ import { createHighlighter } from 'shiki' ;
2+ import * as fs from 'node:fs' ;
3+ import { resolvePath } from './resolve-alias.js' ;
4+
5+ const theme = 'one-dark-pro' ;
6+
7+ const highlighter = await createHighlighter ( {
8+ langs : [ 'svelte' ] ,
9+ themes : [ theme ]
10+ } ) ;
11+
12+ /**
13+ * @param {string } code
14+ * @returns {string }
15+ */
16+ function highlight ( code ) {
17+ const html = highlighter . codeToHtml ( code , {
18+ lang : 'svelte' ,
19+ theme
20+ } ) ;
21+
22+ return html ;
23+ }
24+
25+ /**
26+ * @returns {import("svelte/compiler").PreprocessorGroup }
27+ */
28+ const printComponent = ( ) => {
29+ return {
30+ name : 'print-self' ,
31+ markup ( { content, filename } ) {
32+ const replacement = content . replace ( / % i m p o r t \( ' ( [ ^ ' ] + ) ' \) % / g, ( match , alias ) => {
33+ try {
34+ if ( ! filename ) throw Error ( 'filename is undefined' ) ;
35+
36+ const compPath = resolvePath ( filename , alias ) ;
37+ const content = fs . readFileSync ( compPath , 'utf-8' ) . replace ( '$lib' , 'svelte-knobs' ) . trim ( ) ;
38+ const html = highlight ( content ) ;
39+
40+ return `{@html \`${ html } \`}` ;
41+ } catch ( err ) {
42+ console . error ( err )
43+ return match ;
44+ }
45+ } ) ;
46+
47+ return {
48+ code : replacement
49+ } ;
50+ }
51+ } ;
52+ } ;
53+
54+ export default printComponent ;
Original file line number Diff line number Diff line change 1+ import * as path from 'node:path' ;
2+
3+ const aliasMap = {
4+ $lib : 'src/lib'
5+ } ;
6+
7+ /**
8+ * @param {string } pth
9+ * @returns {string }
10+ */
11+ function resolveAlias ( pth ) {
12+ const entries = Object . entries ( aliasMap ) ;
13+ for ( const [ alias , path ] of entries ) {
14+ if ( ! pth . includes ( alias ) ) continue ;
15+ return pth . replace ( alias , path ) ;
16+ }
17+
18+ return pth ;
19+ }
20+
21+ /**
22+ * @param {string } filename
23+ * @param {string } pth
24+ * @returns {string }
25+ */
26+ export function resolvePath ( filename , pth ) {
27+ if ( pth . startsWith ( './' ) || pth . startsWith ( '../' ) ) {
28+ const currentDir = path . dirname ( filename ) ;
29+ const compPath = path . join ( currentDir , pth ) ;
30+
31+ return compPath ;
32+ }
33+
34+ return resolveAlias ( pth ) ;
35+ }
Original file line number Diff line number Diff line change 1- import adapter from '@sveltejs/adapter-static' ;
21import { vitePreprocess } from '@sveltejs/vite-plugin-svelte' ;
3- import { createHighlighter } from 'shiki' ;
4- import * as path from 'node:path' ;
5- import * as fs from 'node:fs' ;
6-
7- const theme = 'one-dark-pro' ;
8-
9- const highlighter = await createHighlighter ( {
10- langs : [ 'svelte' ] ,
11- themes : [ theme ]
12- } ) ;
13-
14- function highlight ( code ) {
15- const html = highlighter . codeToHtml ( code , {
16- lang : 'svelte' ,
17- theme
18- } ) ;
19-
20- return html ;
21- }
22-
23- function printComponent ( ) {
24- return {
25- name : 'print-self' ,
26- markup ( { content, filename } ) {
27- const replacement = content . replace ( / % i m p o r t \( ' ( [ ^ ' ] + ) ' \) % / g, ( match , pth ) => {
28- const currentDir = path . dirname ( filename ) ;
29- const compPath = path . join ( currentDir , pth ) ;
30-
31- try {
32- const content = fs . readFileSync ( compPath , 'utf-8' ) . replace ( '$lib' , 'svelte-knobs' ) ;
33- const html = highlight ( content ) ;
34-
35- return `{@html \`${ html } \`}` ;
36- } catch {
37- return match ;
38- }
39- } ) ;
40-
41- return {
42- code : replacement
43- } ;
44- }
45- } ;
46- }
2+ import adapter from '@sveltejs/adapter-static' ;
3+ import printComponent from './src-build/print-component.js' ;
474
485/** @type {import('@sveltejs/kit').Config } */
496const config = {
You can’t perform that action at this time.
0 commit comments