履歴: プログラミング/svelte/記事の見た目をいじる
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 6 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2026/12/6 20:20:06 | プログラミング/svelte/記事の見た目をいじる 最新版 | 9,114 | +1 | -1 | 表示 | 差分 | — |
| 2023/12/4 7:34:10 | プログラミング/svelte/記事の見た目をいじる | 9,114 | +3 | -0 | 表示 | 差分 | 選択 |
| 2023/12/1 11:35:57 | プログラミング/svelte/記事の見た目をいじる | 9,031 | +44 | -96 | 表示 | 表示中 | 選択 |
| 2023/12/1 8:11:08 | プログラミング/svelte/記事の見た目をいじる | 9,624 | +233 | -0 | 表示 | 差分 | 選択 |
| 2023/11/30 17:59:26 | プログラミング/svelte/記事の見た目をいじる | 3,410 | +111 | -0 | 表示 | 差分 | 基準 |
| 2023/11/30 15:59:54 | プログラミング/svelte/記事の見た目をいじる | 640 | — | — | 表示 | — | 選択 |
基準: プログラミング/svelte/記事の見た目をいじる (2023/11/30 17:59:26) → 比較: プログラミング/svelte/記事の見た目をいじる (2023/12/1 11:35:57)
@@ -141,4 +141,185 @@
痒い所に手が届くよう、marked-highlight を使わず自分でやろう。
+- highlightjs-svelte
+- highlightjs-cobol
+- highlightjs-iptables
+- highlightjs-vba
+
+あたりが気になるけど、さしあたりは svelte だけ入れとく。
+
+ LANG: console
$ pnpm rm marked-highlight
+ $ pnpm i highlightjs-svelte
+ $ pnpm add --save-dev @types/highlightjs-svelte
+ ERR_PNPM_FETCH_404 GET https://registry.npmjs.org/@types%2Fhighlightjs-svelte: Not Found - 404
+
+ This error happened while installing a direct dependency of C:\Users\osamu\Desktop\svelte\authtest
+
+ @types/highlightjs-svelte is not in the npm registry, or you have no permission to fetch it.
+
+ No authorization header was set for the request.
+ Progress: resolved 42, reused 42, downloaded 0, added 0
+
+あーと、highlightjs-svelte にはタイプ定義が提供されていない。
+
+仕方がないので、
+
+src/node_modules/@types/highlightjs-svelte.d.ts
+ LANG: ts
+ import type { HLJSApi } from 'highlight.js';
+
+ declare function hljs_svelte(hljs: HLJSApi): void;
+ export default hljs_svelte;
+
+を作成した。どうやら src/@types ではだめで src/node_module/@types じゃないと読み込んでくれないらしい。
+
+で、これだと .gitignore の node_module というルールに引っ掛かってしまうので、
+
+.gitignore
+ - node_modules
+ + node_modules/*
+ + !/src/node_modules/@types/*
+
+として、このフォルダの中身だけ特別にコミットするようにした。
+
+** marked のエクステンション codeRenderer を作る [#z7d15cbe]
+
+コンポーネントからはこのように使える。
+
+src/lib/components/ArticleElement.svelte
+ LANG: html
+ <style context="module">
+ + import { codeRenderer } from './codeRenderer';
+ + import { decolatorsInfo } from './codeDecolators/codeDecolatorInfo';
+ +
+ + // 各デコレータが追加する css をまとめる
+ + const codeRendererCss = decolatorsInfo.map(info=>info.css).join("\n");
+
+ ...
+
+ // シンタックスハイライト
+ + marked.use(codeRenderer);
+ ...
+
+ </script>
+
+ + <svelte:head>
+ + {@html '<style>' + codeRendererCss + '</style>'}
+ + </svelte:head>
+
+decolatorsInfo には孫ライブラリが必要な css を個別に追加するので、
+それを繋げてヘッダーに入れている。
+
+ LANG: html
+ <style>{@html codeRendererCss}</style>
+
+としたのでは動かなかった。<style> タグ自体を文字列として生成したらうまくいった。
+
+codeRenderer は marked からコードブロックごとに呼び出されることになる。
+
+デコレータ的なアルゴリズムで
+
+- rowsDecorator rows(50) などとして表示行数を変更できる
+- numDecorator 行番号付与
+- diffDecorator diff 的な +/- 行に色を付ける
+- splitterDecorator 行ごとに処理できるよう改行単位でタグを閉じる
+- highlightDecorator シンタックスハイライト
+
+などを各 Decorator に分業させてコードブロックを処理する。
+
+src/lib/components/codeRenderer.ts
+ LANG: ts
+ import { rowsDecorator } from './codeDecolators/rowsDecorator';
+ import { numDecorator } from './codeDecolators/numDecorator';
+ import { diffDecorator } from './codeDecolators/diffDecorator';
+ import { splitterDecorator } from './codeDecolators/splitterDecorator';
+ import { highlightDecorator } from './codeDecolators/highlightDecorator';
+
+ // HTML の特殊文字をエスケープ
+ // https://stackoverflow.com/questions/1787322/what-is-the-htmlspecialchars-equivalent-in-javascript
+ export function escapeHtml(text: string) {
+ const map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
+ return text.replace(/[&<>"']/g, (c) => map[c as keyof typeof map]);
+ }
+
+ function unescapeHtml(text: string) {
+ const map = { '&':'&', '<':'<', '>':'>', '"':'"', ''':"'" };
+ return text.replace(/&(?:amp|lt|gt|quot|#039);/g, (code) => map[code as keyof typeof map]);
+ }
+
+ //====================
+ // CodeBlockDecolator
+
+ export declare type CodeBlock = {
+ code: string,
+ info: string[],
+ params: string[],
+ tags: string[],
+ codeClasses: string[],
+ maxRows?: number,
+ rawHtml?: string, // ここに代入されればそのままそれを出力する
+ };
+
+ // デコレータ鎖は先頭から実行される
+ export declare type Decolator = (chain: Decolator[], block: CodeBlock) => CodeBlock;
+
+ const decolators: Decolator[] = [
+ rowsDecorator,
+ numDecorator,
+ diffDecorator,
+ splitterDecorator, // 行ごとに分けられるようにするため改行の前後でタグを閉じる・開く
+ highlightDecorator, // highlight.js を呼び出す
+ ];
+
+ export function callDecolatorChain(chain: Decolator[], block: CodeBlock) {
+ const first = chain[0];
+ return first ? first(chain.slice(1), block) : block;
+ }
+
+ //====================
+ // code ブロックをレンダリングする
+
+ export const codeRenderer = {renderer:{
+ code: (code: string, infoString = '', escaped: boolean) => {
+
+ if(escaped) {
+ code = unescapeHtml(code);
+ }
+
+ // infoString は info param0 param1 param2
+ // info は some:some:some
+ const params = infoString.split(' ');
+ const info = params.length ? params.shift()!.split(':') : [];
+ const block = callDecolatorChain(decolators, {code, info, params, tags: [], codeClasses: []});
+
+ return block.rawHtml ? block.rawHtml
+ : `<pre class="marked-code">`
+ + (block.tags.length ? `<span class="tags">${block.tags.join(' ')}</span>` : '')
+ + `<code class="${block.codeClasses.join(' ')}" style="max-height:${block.maxRows || 30}lh">${block.code}</code>`
+ + `</pre>`;
+ }
+ }};
+
+CodeBlock に新しいオプションを追加したり、~
+Decorator を増やしたりすることで、~
+ブロックの内容を画像にして表示するなど、ブロック要素プラグインのような機能をどんどん追加できるはず。
+
+ここでは個別のコードは載せないが、かなりいろいろ表示できるようになった。
+
+ LANG: console
+ $ git add . && git commit -m "コードブロックの表示を改善した"
+ modified: .gitignore
+ modified: package.json
+ modified: pnpm-lock.yaml
+ new file: src/lib/components/ArticleElement.svelte
+ new file: src/lib/components/codeDecolators/codeDecolatorInfo.ts
+ new file: src/lib/components/codeDecolators/diffDecorator.ts
+ new file: src/lib/components/codeDecolators/highlightDecorator.ts
+ new file: src/lib/components/codeDecolators/numDecorator.ts
+ new file: src/lib/components/codeDecolators/rowsDecorator.ts
+ new file: src/lib/components/codeDecolators/splitterDecorator.ts
+ new file: src/lib/components/codeRenderer.ts
+ new file: src/node_modules/@types/highlightjs-svelte.d.ts
+ modified: src/routes/articles/[...titleOrId]/+page.svelte
+