Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #29

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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