-
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.
Merge pull request #22 from taga3s/feature/show-filename-with-rehype-…
…momiji feat: show filename ui above the code block
- Loading branch information
Showing
5 changed files
with
119 additions
and
48 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,43 @@ | ||
import { bundledLanguages, bundledThemes, type BundledLanguage, type BundledTheme } from "shiki"; | ||
import { getHighlighter } from "./highlighter"; | ||
import { toHtml } from "hast-util-to-html"; | ||
import type { Element } from "hast"; | ||
|
||
const defaultHighlighter = await getHighlighter({ themes: bundledThemes, langs: bundledLanguages }); | ||
|
||
const buildCodeBlockHTML = (rawCode: string, lang: string, filename: string, theme?: string) => { | ||
const hast = defaultHighlighter.codeToHast(rawCode, { | ||
lang: lang, | ||
theme: theme ?? "github-dark", | ||
}); | ||
|
||
const targetElem = hast.children[0] as Element; | ||
|
||
// Add filename to the code block if it exists | ||
if (filename !== "" && targetElem) { | ||
targetElem.properties = { | ||
class: targetElem.properties.class, | ||
style: `${targetElem.properties.style}; padding-top: 12px;`, | ||
tabindex: targetElem.properties.tabindex, | ||
}; | ||
|
||
targetElem.children.unshift({ | ||
type: "element", | ||
tagName: "div", | ||
properties: { | ||
style: | ||
"width: fit-content; margin-bottom: 16px; padding: 4px 8px; font-size: 14px; color: white; background-color: gray;", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: filename, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
return toHtml(hast); | ||
}; | ||
|
||
export { buildCodeBlockHTML }; |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.