Skip to content

Commit

Permalink
Merge pull request #29 from taga3s/refactor/rehype-momiji
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
taga3s authored Sep 15, 2024
2 parents 554dd82 + 9a61abc commit 525f49f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
13 changes: 10 additions & 3 deletions app/modules/rehype-momiji/buildCodeBlockHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { COLOR_SHIRONERI, COLOR_HAI } from "./colors";
import { visit } from "unist-util-visit";

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: BundledTheme) => {
const buildCodeBlockHTML = (
rawCode: string,
lang: string,
theme: BundledTheme,
filename: string,
filenameBGColor?: string,
filenameTextColor?: string,
) => {
const hast = defaultHighlighter.codeToHast(rawCode, {
lang: lang,
theme: theme,
});
const filenameColorStyle = `background-color: ${filenameBGColor ?? COLOR_HAI}; color: ${filenameTextColor ?? COLOR_SHIRONERI};`;

// Add filename to the code block if it exists
if (filename === "") {
Expand All @@ -36,7 +43,7 @@ const buildCodeBlockHTML = (rawCode: string, lang: string, filename: string, the
type: "element",
tagName: "div",
properties: {
style: `width: fit-content; margin-bottom: 16px; padding: 4px 8px; font-size: 14px; border-radius: 0 0 4px 4px; ${defaultFilenameUIColor};`,
style: `width: fit-content; margin-bottom: 16px; padding: 4px 8px; font-size: 14px; border-radius: 0 0 4px 4px; ${filenameColorStyle}`,
},
children: [
{
Expand Down
22 changes: 17 additions & 5 deletions app/modules/rehype-momiji/rehypeMomiji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ 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;
filenameBGColor?: string;
filenameTextColor?: string;
};

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

const rehypeMomiji: Plugin = (options: Options = { theme: "github-dark-default", fallbackLang: "c" }) => {
const rehypeMomiji: Plugin = (options: Options = {}) => {
const langs = defaultHighlighter.getLoadedLanguages();
const { theme, fallbackLang } = options;
const { theme = "github-dark-default", fallbackLang = "c", filenameBGColor, filenameTextColor } = options;

const parseLanguage = (classNames: string[]): string | undefined => {
for (const className of classNames) {
Expand Down Expand Up @@ -65,15 +70,22 @@ const rehypeMomiji: Plugin = (options: Options = { theme: "github-dark-default",

const filename = (codeElem.properties["data-remark-code-filename"] as string) ?? "";

const highlightCode = buildCodeBlockHTML(rawCode, supportedLang, filename, theme);
const highlightCode = buildCodeBlockHTML(
rawCode,
supportedLang,
theme,
filename,
filenameBGColor,
filenameTextColor,
);

const container = `
<div style="display: flex; flex-direction: column; gap: 2px;">
${highlightCode}
</div>
`;

const parsedRoot = parser.parse(container) as Root;
const parsedRoot = parser.parse(container);
node.tagName = "div";
node.children = parsedRoot.children as Element[];
});
Expand Down
4 changes: 2 additions & 2 deletions app/routes/posts/md_test.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Markdown試し打ち"
publishedAt: "2024/09/14"
title: Markdown試し打ち
publishedAt: 2024/09/14
---

# # Section1
Expand Down

0 comments on commit 525f49f

Please sign in to comment.