-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move preprocessors to src-build/
- Loading branch information
Showing
3 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createHighlighter } from 'shiki'; | ||
import * as fs from 'node:fs'; | ||
import { resolvePath } from './resolve-alias.js'; | ||
|
||
const theme = 'one-dark-pro'; | ||
|
||
const highlighter = await createHighlighter({ | ||
langs: ['svelte'], | ||
themes: [theme] | ||
}); | ||
|
||
/** | ||
* @param {string} code | ||
* @returns {string} | ||
*/ | ||
function highlight(code) { | ||
const html = highlighter.codeToHtml(code, { | ||
lang: 'svelte', | ||
theme | ||
}); | ||
|
||
return html; | ||
} | ||
|
||
/** | ||
* @returns {import("svelte/compiler").PreprocessorGroup} | ||
*/ | ||
const printComponent = () => { | ||
return { | ||
name: 'print-self', | ||
markup({ content, filename }) { | ||
const replacement = content.replace(/%import\('([^']+)'\)%/g, (match, alias) => { | ||
try { | ||
if (!filename) throw Error('filename is undefined'); | ||
|
||
const compPath = resolvePath(filename, alias); | ||
const content = fs.readFileSync(compPath, 'utf-8').replace('$lib', 'svelte-knobs').trim(); | ||
const html = highlight(content); | ||
|
||
return `{@html \`${html}\`}`; | ||
} catch (err) { | ||
console.error(err) | ||
return match; | ||
} | ||
}); | ||
|
||
return { | ||
code: replacement | ||
}; | ||
} | ||
}; | ||
}; | ||
|
||
export default printComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as path from 'node:path'; | ||
|
||
const aliasMap = { | ||
$lib: 'src/lib' | ||
}; | ||
|
||
/** | ||
* @param {string} pth | ||
* @returns {string} | ||
*/ | ||
function resolveAlias(pth) { | ||
const entries = Object.entries(aliasMap); | ||
for (const [alias, path] of entries) { | ||
if (!pth.includes(alias)) continue; | ||
return pth.replace(alias, path); | ||
} | ||
|
||
return pth; | ||
} | ||
|
||
/** | ||
* @param {string} filename | ||
* @param {string} pth | ||
* @returns {string} | ||
*/ | ||
export function resolvePath(filename, pth) { | ||
if (pth.startsWith('./') || pth.startsWith('../')) { | ||
const currentDir = path.dirname(filename); | ||
const compPath = path.join(currentDir, pth); | ||
|
||
return compPath; | ||
} | ||
|
||
return resolveAlias(pth); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters