Skip to content

Commit 5024ee1

Browse files
authored
Merge pull request #26 from ut-code/codeblock-style
コードブロックのフォントや見た目をエディター・ターミナルに合わせる
2 parents 9916d88 + f4bf965 commit 5024ee1

File tree

8 files changed

+43
-38
lines changed

8 files changed

+43
-38
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ npm run lint
8383

8484
- [Next.js](https://nextjs.org/docs)
8585
- 検索する際は「App Router」を含めることで古い記事に惑わされることが少なくなります。
86+
- [OpenNext](https://opennext.js.org/cloudflare)
8687
- [DaisyUI](https://daisyui.com/docs/use/) / [Tailwind CSS](https://tailwindcss.com/docs)
8788
- buttonやinputやメニューなどの基本的なコンポーネントのデザインはDaisyUIにあるものを使うと楽です
8889
- 細かくスタイルを調整したい場合はTailwind CSSを使います (CSS直接指定(`style={{...}}`)よりもちょっと楽に書ける)
8990
- よくわからなかったらstyle直接指定でも良い
91+
- [SWR](https://swr.vercel.app/ja)
9092
- [react-markdown](https://www.npmjs.com/package/react-markdown)
91-
- オプションがいろいろあり、今はほぼデフォルト設定で突っ込んでいるがあとでなんとかする
92-
- [OpenNext](https://opennext.js.org/cloudflare)
93+
- REPL・実行結果表示: [xterm.js](https://xtermjs.org/)
94+
- コードエディター: [react-ace](https://github.com/securingsincity/react-ace)
95+
- それ以外のコードブロック: [react-syntax-highlighter](https://github.com/react-syntax-highlighter/react-syntax-highlighter)

app/[docs_id]/markdown.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Markdown, { Components } from "react-markdown";
22
import remarkGfm from "remark-gfm";
3-
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
3+
import SyntaxHighlighter from "react-syntax-highlighter";
44
import { PythonEmbeddedTerminal } from "../terminal/python/embedded";
55
import { Heading } from "./section";
66
import { AceLang, EditorComponent } from "../terminal/editor";
77
import { ExecFile, ExecLang } from "../terminal/exec";
8+
import { tomorrow } from "react-syntax-highlighter/dist/esm/styles/hljs";
89

910
export function StyledMarkdown({ content }: { content: string }) {
1011
return (
@@ -138,8 +139,8 @@ const components: Components = {
138139
<SyntaxHighlighter
139140
language={match[1]}
140141
PreTag="div"
141-
className="border border-base-300 mx-2 my-2 rounded-lg text-sm! m-2! p-4!"
142-
// style={todo dark theme?}
142+
className="border border-base-content/50 mx-2 my-2 rounded-lg text-sm p-4!"
143+
style={tomorrow} // todo dark theme (editor.tsx で指定したのと同じテーマを選ぶようにすること)
143144
{...props}
144145
>
145146
{String(props.children || "").replace(/\n$/, "")}
@@ -150,8 +151,8 @@ const components: Components = {
150151
return (
151152
<SyntaxHighlighter
152153
PreTag="div"
153-
className="border border-base-300 mx-2 my-2 rounded-lg text-sm! m-2! p-4!"
154-
// style={todo dark theme?}
154+
className="border border-base-content/50 mx-2 my-2 rounded-lg text-sm p-4!"
155+
style={tomorrow} // todo dark theme
155156
{...props}
156157
>
157158
{String(props.children || "").replace(/\n$/, "")}

app/globals.css

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
@import "tailwindcss";
22
@plugin "daisyui";
33

4-
/* inconsolata-latin-wght-normal */
5-
@font-face {
6-
font-family: "Inconsolata Variable";
7-
font-style: normal;
8-
font-display: swap;
9-
font-weight: 200 900;
10-
src: url(https://cdn.jsdelivr.net/fontsource/fonts/inconsolata:vf@latest/latin-wght-normal.woff2)
11-
format("woff2-variations");
12-
unicode-range:
13-
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
14-
U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212,
15-
U+2215, U+FEFF, U+FFFD;
16-
}
17-
4+
/* CDNからダウンロードするURLを指定したらなんかエラー出るので、npmでインストールしてlayout.tsxでimportすることにした */
185
@theme {
19-
--font-mono: "Inconsolata Variable", monospace;
6+
--font-mono: "Inconsolata Variable", "Noto Sans JP Variable", monospace;
207
}

app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Metadata } from "next";
2+
import "@fontsource-variable/noto-sans-jp";
3+
import "@fontsource-variable/inconsolata";
24
import "./globals.css";
35
import { Navbar } from "./navbar";
46
import { Sidebar } from "./sidebar";

app/terminal/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function EditorComponent(props: EditorProps) {
8787
<AceEditor
8888
name={`ace-editor-${props.filename}`}
8989
mode={props.language}
90-
theme="tomorrow" // TODO dark theme
90+
theme="tomorrow" // TODO dark theme (twilightがいいんじゃないかと勝手に思っている)
9191
tabSize={props.tabSize}
9292
width="100%"
9393
height={

app/terminal/terminal.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,9 @@ export function useTerminal(props: TerminalProps) {
7171
useEffect(() => {
7272
const abortController = new AbortController();
7373

74-
(async () => {
75-
// globals.cssでフォントを指定し読み込んでいるが、
76-
// それが読み込まれる前にterminalを初期化してしまうとバグる。
77-
// なのでここでフォントをfetchし成功するまでterminalの初期化は待つ
78-
try {
79-
await fetch(
80-
"https://cdn.jsdelivr.net/fontsource/fonts/inconsolata:vf@latest/latin-wght-normal.woff2",
81-
{ signal: abortController.signal }
82-
);
83-
} catch {
84-
// ignore
85-
}
86-
74+
// globals.cssでフォントを指定し読み込んでいるが、
75+
// それが読み込まれる前にterminalを初期化してしまうとバグる。
76+
document.fonts.load("0.875rem Inconsolata Variable").then(() => {
8777
if (!abortController.signal.aborted) {
8878
const fromCSS = (varName: string) =>
8979
window.getComputedStyle(document.body).getPropertyValue(varName);
@@ -96,7 +86,7 @@ export function useTerminal(props: TerminalProps) {
9686
fontSize: 14,
9787
lineHeight: 1.4,
9888
letterSpacing: 0,
99-
fontFamily: "Inconsolata Variable",
89+
fontFamily: "'Inconsolata Variable','Noto Sans JP Variable'",
10090
theme: {
10191
// DaisyUIの変数を使用してテーマを設定している
10292
// TODO: ダークテーマ/ライトテーマを切り替えたときに再設定する?
@@ -134,7 +124,7 @@ export function useTerminal(props: TerminalProps) {
134124
setTermReady(true);
135125
onReadyRef.current?.();
136126
}
137-
})();
127+
});
138128

139129
const observer = new ResizeObserver(() => {
140130
// fitAddon.fit();

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
1515
},
1616
"dependencies": {
17+
"@fontsource-variable/inconsolata": "^5.2.6",
18+
"@fontsource-variable/noto-sans-jp": "^5.2.6",
1719
"@google/generative-ai": "^0.24.1",
1820
"@opennextjs/cloudflare": "^1.6.3",
1921
"@xterm/addon-fit": "^0.11.0-beta.115",

0 commit comments

Comments
 (0)