Skip to content

Commit

Permalink
Merge pull request #23 from taga3s/fix/codeblock-filename-ui-styling
Browse files Browse the repository at this point in the history
fix: filename ui styling
  • Loading branch information
taga3s authored Sep 14, 2024
2 parents eec5985 + bb8d85e commit f84698a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/modules/rehype-momiji/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
## features

- Highlighting Code
- Displaying Filename
13 changes: 7 additions & 6 deletions app/modules/rehype-momiji/buildCodeBlockHTML.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { bundledLanguages, bundledThemes, type BundledLanguage, type BundledTheme } from "shiki";
import { bundledLanguages, bundledThemes, type BundledTheme } from "shiki";
import { getHighlighter } from "./highlighter";
import { toHtml } from "hast-util-to-html";
import type { Element } from "hast";
import { COLOR_SHIRONERI, COLOR_HAI } from "./colors";

const defaultHighlighter = await getHighlighter({ themes: bundledThemes, langs: bundledLanguages });
const defaultFilenameUIColor = `color: ${COLOR_SHIRONERI}; background-color: ${COLOR_HAI};`;

const buildCodeBlockHTML = (rawCode: string, lang: string, filename: string, theme?: string) => {
const buildCodeBlockHTML = (rawCode: string, lang: string, filename: string, theme: BundledTheme) => {
const hast = defaultHighlighter.codeToHast(rawCode, {
lang: lang,
theme: theme ?? "github-dark",
theme: theme,
});

const targetElem = hast.children[0] as Element;
Expand All @@ -17,16 +19,15 @@ const buildCodeBlockHTML = (rawCode: string, lang: string, filename: string, the
if (filename !== "" && targetElem) {
targetElem.properties = {
class: targetElem.properties.class,
style: `${targetElem.properties.style}; padding-top: 12px;`,
style: `${targetElem.properties.style}; padding-top: 0px;`,
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;",
style: `width: fit-content; margin-bottom: 16px; padding: 4px 8px; font-size: 14px; border-radius: 0 0 4px 4px; ${defaultFilenameUIColor};`,
},
children: [
{
Expand Down
6 changes: 6 additions & 0 deletions app/modules/rehype-momiji/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const COLOR_SUMI = "#1C1C1C;";
const COLOR_SHIRONERI = "#FCFAF2";
const COLOR_HAI = "#828282";
const COLOR_GOFUN = "#FFFFFB";

export { COLOR_SUMI, COLOR_SHIRONERI, COLOR_HAI, COLOR_GOFUN };
2 changes: 1 addition & 1 deletion app/modules/rehype-momiji/rehypeMomiji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { parser } from "./parser";
import { isArray, isObject, isString } from "./utils/checkTypeOfOperandValue";
import { buildCodeBlockHTML } from "./buildCodeBlockHTML";

type Options = { theme?: BundledTheme; fallbackLang?: BundledLanguage };
type Options = { theme: BundledTheme; fallbackLang?: BundledLanguage };

const defaultHighlighter = await getHighlighter({ themes: bundledThemes, langs: bundledLanguages });

Expand Down

0 comments on commit f84698a

Please sign in to comment.