履歴: プログラミング/svelte
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 35 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2024/9/7 0:08:14 | プログラミング/svelte 最新版 | 5,314 | +5 | -2 | 表示 | 差分 | — |
| 2024/9/6 18:23:34 | プログラミング/svelte | 5,216 | +6 | -0 | 表示 | 差分 | 選択 |
| 2024/8/30 19:02:58 | プログラミング/svelte | 5,042 | +2 | -0 | 表示 | 差分 | 選択 |
| 2024/7/30 3:52:46 | プログラミング/svelte | 4,990 | +1 | -0 | 表示 | 差分 | 選択 |
| 2024/7/21 3:07:50 | プログラミング/svelte | 4,945 | +1 | -1 | 表示 | 差分 | 選択 |
| 2024/7/19 0:55:05 | プログラミング/svelte | 4,911 | +1 | -0 | 表示 | 差分 | 選択 |
| 2024/7/2 7:41:30 | プログラミング/svelte | 4,878 | +112 | -9 | 表示 | 差分 | 選択 |
| 2024/7/1 11:29:30 | プログラミング/svelte | 2,417 | +3 | -0 | 表示 | 差分 | 選択 |
| 2023/12/3 2:48:52 | プログラミング/svelte | 2,360 | +15 | -0 | 表示 | 差分 | 選択 |
| 2023/12/2 0:45:57 | プログラミング/svelte | 2,026 | +1 | -0 | 表示 | 差分 | 選択 |
| 2023/11/30 15:59:20 | プログラミング/svelte | 1,988 | +3 | -30 | 表示 | 差分 | 選択 |
| 2023/11/29 18:54:52 | プログラミング/svelte | 2,505 | +17 | -0 | 表示 | 差分 | 選択 |
| 2023/11/29 3:15:43 | プログラミング/svelte | 2,101 | +1 | -0 | 表示 | 差分 | 選択 |
| 2023/11/28 10:21:04 | プログラミング/svelte | 2,066 | +10 | -4390 | 表示 | 差分 | 選択 |
| 2023/11/27 15:12:30 | プログラミング/svelte | 136,600 | +364 | -1 | 表示 | 表示中 | 選択 |
| 2023/11/27 7:26:06 | プログラミング/svelte | 127,338 | +284 | -0 | 表示 | 差分 | 選択 |
| 2023/11/26 16:26:54 | プログラミング/svelte | 118,533 | +675 | -15 | 表示 | 差分 | 基準 |
| 2023/11/26 10:53:18 | プログラミング/svelte | 93,589 | +50 | -6 | 表示 | 差分 | 選択 |
| 2023/11/25 1:44:35 | プログラミング/svelte | 91,814 | +66 | -0 | 表示 | 差分 | 選択 |
| 2023/11/24 16:23:08 | プログラミング/svelte | 90,481 | +460 | -3 | 表示 | 差分 | 選択 |
| 2023/11/24 8:25:16 | プログラミング/svelte | 76,694 | +213 | -23 | 表示 | 差分 | 選択 |
| 2023/11/23 22:42:10 | プログラミング/svelte | 66,195 | +7 | -7 | 表示 | 差分 | 選択 |
| 2023/11/23 12:53:25 | プログラミング/svelte | 66,212 | +18 | -29 | 表示 | 差分 | 選択 |
| 2023/11/23 9:32:58 | プログラミング/svelte | 66,711 | +512 | -38 | 表示 | 差分 | 選択 |
| 2023/11/23 0:44:28 | プログラミング/svelte | 52,259 | +300 | -55 | 表示 | 差分 | 選択 |
| 2023/11/22 14:35:58 | プログラミング/svelte | 45,266 | +81 | -0 | 表示 | 差分 | 選択 |
| 2023/11/22 7:59:00 | プログラミング/svelte | 43,056 | +124 | -5 | 表示 | 差分 | 選択 |
| 2023/11/22 0:32:39 | プログラミング/svelte | 39,942 | +210 | -86 | 表示 | 差分 | 選択 |
| 2023/11/21 14:18:13 | プログラミング/svelte | 36,876 | +93 | -5 | 表示 | 差分 | 選択 |
| 2023/11/21 11:45:28 | プログラミング/svelte | 34,915 | +825 | -5 | 表示 | 差分 | 選択 |
| 2023/7/5 14:16:38 | プログラミング/svelte | 12,623 | +1 | -1 | 表示 | 差分 | 選択 |
| 2023/6/1 13:11:31 | プログラミング/svelte | 12,622 | +12 | -1 | 表示 | 差分 | 選択 |
| 2023/6/1 6:47:49 | プログラミング/svelte | 12,354 | +412 | -2 | 表示 | 差分 | 選択 |
| 2023/5/22 9:38:55 | プログラミング/svelte | 592 | +9 | -0 | 表示 | 差分 | 選択 |
| 2023/5/15 2:26:56 | プログラミング/svelte | 271 | — | — | 表示 | — | 選択 |
基準: プログラミング/svelte (2023/11/26 16:26:54) → 比較: プログラミング/svelte (2023/11/27 15:12:30)
@@ -20,6 +20,10 @@
https://tech.smartcamp.co.jp/entry/started-prisma
- worst password list~
https://github.com/danielmiessler/SecLists/blob/master/Passwords/Common-Credentials/10-million-password-list-top-100000.txt
+- 5歳娘「パパ、余分なpropsいっぱい書くんだね!」~
+https://qiita.com/Yametaro/items/814f40d08e9d30584e20#5%E6%AD%B3%E5%A8%98%E3%83%91%E3%83%91%E4%BD%99%E5%88%86%E3%81%AAprops%E3%81%84%E3%81%A3%E3%81%B1%E3%81%84%E6%9B%B8%E3%81%8F%E3%82%93%E3%81%A0%E3%81%AD
+-- svelte での書き方 → https://stackoverflow.com/questions/76285794/svelte-components-intrinsicelements-or-sveltehtmlelements-for-extending-with-ht
+
* VS Code + Git Bash + nvm のインストール [#d5c705f1]
https://blog.css-net.co.jp/entry/dev-environment-windows#2-2-Git-for-Windows
@@ -3714,6 +3718,12 @@
Done in 6.6s
+
+TailwindCSS を使うと、
+
+- @apply が使える https://tailwindcss.com/docs/reusing-styles#extracting-classes-with-apply
+-- class の add/remove を css だけでできる感じになる
+
*** コンポーネントの準備 [#g6e8a392]
src/lib/components/DialogPage.svelte
@@ -3795,3 +3805,640 @@
LANG: console
$ pnpm dlx mrm@2 lint-staged
+ .../Local/pnpm/store/v3/tmp/dlx-29384 | +234 +++++++++++++++++++++++
+ .../Local/pnpm/store/v3/tmp/dlx-29384 | Progress: resolved 234, reused 234, downloaded 0, added 234, done
+ Running lint-staged...
+ Installing lint-staged and husky...
+ npm ERR! Cannot read properties of null (reading 'matches')
+
+ npm ERR! A complete log of this run can be found in:
+ npm ERR! C:\Users\osamu\AppData\Local\npm-cache\_logs\2023-11-26T16_26_28_836Z-debug-0.log
+ husky - Git hooks installed
+ husky - updated .husky/pre-commit
+
+あれ、素直に npx を使うべきだった?
+
+ LANG: console
+ $ npx mrm@2 lint-staged
+ Running lint-staged...
+ Installing lint-staged and husky...
+ ...
+ [#.................] / idealTree:@jridgewell/sourcemap-codec: timing idealTree:node_modules/.pnpm/@jridgewell+sourcemap-codec@1.4.15/node_modules/@jridgewell/sourcemap-codec Completed in
+ npm ERR! Cannot read properties of null (reading 'matches')
+
+ npm ERR! A complete log of this run can be found in:
+ npm ERR! C:\Users\osamu\AppData\Local\npm-cache\_logs\2023-11-27T03_07_53_172Z-debug-0.log
+ husky - Git hooks installed
+ husky - updated .husky/pre-commit
+
+うまく行かない。
+
+後回しにしよう。。。
+
+っと思ったら中途半端に変更されてて commit でエラーになる。
+
+ LANG: console
+ $ git commit
+ node:internal/modules/cjs/loader:1078
+ throw err;
+ ^
+
+ Error: Cannot find module 'C:\Users\osamu\Desktop\svelte\authtest\node_modules\lint-staged\bin\lint-staged.js'
+ at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
+ at Module._load (node:internal/modules/cjs/loader:920:27)
+ at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
+ at node:internal/main/run_main_module:23:47 {
+ code: 'MODULE_NOT_FOUND',
+ requireStack: []
+ }
+
+ Node.js v18.16.0
+ husky - pre-commit hook exited with code 1 (error)
+
+ $ cat .git/config
+ [core]
+ repositoryformatversion = 0
+ filemode = false
+ bare = false
+ logallrefupdates = true
+ symlinks = false
+ ignorecase = true
+ hooksPath = .husky
+
+ $ ls .husky/*
+ .husky/pre-commit*
+
+ .husky/_:
+ husky.sh*
+ $ cat .husky/pre-commit
+ #!/bin/sh
+ . "$(dirname "$0")/_/husky.sh"
+
+ npx lint-staged
+ $ pnpm lint-staged
+ node:internal/modules/cjs/loader:1078
+ throw err;
+ ^
+
+ Error: Cannot find module 'C:\Users\osamu\Desktop\svelte\authtest\node_modules\lint-staged\bin\lint-staged.js'
+ at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
+ at Module._load (node:internal/modules/cjs/loader:920:27)
+ at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
+ at node:internal/main/run_main_module:23:47 {
+ code: 'MODULE_NOT_FOUND',
+ requireStack: []
+ }
+
+ Node.js v18.16.0
+ ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL Command failed with exit code 1: lint-staged
+
+ Command "lint-staged" not found.
+
+あれ? さしあたり lint-staged が入ってなきゃうまく行くわけない感じ???~
+たぶん husky もなんじゃ???
+
+ LANG: console
+ $ pnpm i -D lint-staged husky
+ $ pnpm up
+
+これで git commit で prettier が走るようになった。
+
+** routes を整理する? [#n6346265]
+
+- /account/(loggedOut)/new アカウント登録
+- /account/(loggedOut)/new/[token] アカウント登録
+- /account/(loggedOut)/reset パスワードリセット
+- /account/(loggedOut)/reset/[token] パスワードリセット
+- /account/(loggedIn)/edit 登録情報編集
+- /account/(loggedIn)/delete 登録抹消
+- /account/(loggedIn)/email メールアドレス変更
+- /account/(loggedIn)/email/[token] メールアドレス変更
+- /session/(loggedOut)/new ログイン
+- /session/(loggedIn)/delete ログアウト
+
+とか。
+
+検討中。
+
+* Markdown でブログ的なものを作ってみる [#x6384456]
+
+上の内容の続き。
+
+** データベースの準備 [#ua1c2e03]
+
+prisma/schema.prisma
+ model AuthUser {
+ ...
+
+ roles Role[]
+ + articles Article[]
+ @@map("auth_user")
+ }
+ ...
+
+ + model Article {
+ + id String @id @default(uuid())
+ + author AuthUser @relation(fields: [authorId], references: [id])
+ + authorId String @map("author_id")
+ + title String
+ + slug String? @unique
+ + body String
+ + newRevisionId String? @unique @map("new_revision_id")
+ + oldRevision Article? @relation("ArticleRevision", fields: [newRevisionId], references: [id])
+ + newRevision Article? @relation("ArticleRevision")
+ + createdAt DateTime @default(now()) @map("created_at")
+ + deletedAt DateTime? @default(now()) @map("deleted_at")
+ + attachments Attachment[]
+ + }
+ +
+ + model Attachment {
+ + id String @ id @default(uuid())
+ + article Article @relation(fields: [articleId], references: [id])
+ + articleId String
+ + body Bytes
+ + createdAt DateTime @default(now()) @map("created_at")
+ + modifiedAt DateTime @default(now()) @map("modified_at")
+ + }
+
+newRevisionId がなく deletedAt もないものが最新の記事ということになる
+
+最も新しいものだけに slug を付与する。
+
+- slug 化について~
+https://qiita.com/Yarakashi_Kikohshi/items/af0b334af1a1e73a5661#-url-%E3%83%95%E3%83%A9%E3%82%B0%E3%83%A1%E3%83%B3%E3%83%88%E8%AD%98%E5%88%A5%E5%AD%90
+- https://www.npmjs.com/package/slugify
+
+ただし、古い版から slug が変化する場合には古い版の slug も残しておき、
+そこから最新版へリダイレクトする。
+
+articles/123 のように数字でアクセスした場合には id で検索して
+その最新版へリダイレクトする。
+
+StackOverflow などでは /questions/[id]/[slug] としていて、
+[slug] の有無にかかわらず [id] まででアクセスできるみたい。
+[slug] は任意の文字列で構わない。
+
+SEO 的に [slug] が付いていることだけが重要なのだとすればこれが一番いい。
+古い番号からは 301 Moved Permanently で新しい番号に飛べばいい。
+
+その方針なら slug をデータベースに持たせる必要はないのか。
+
+prisma/schema.prisma
+ model Article {
+ ...
+
+ - slug String? @unique
+
+newRevision の投稿時に Attachment をどのように引き継ぐかが問題?
+
+どうせ表示の時にしか必要にならないので、表示の際に古いものも含めて全部列挙すればそれでよいかも?
+
+ LANG: console
+ $ pnpm prisma migrate dev --name "add Article and Attachment"
+ Environment variables loaded from .env
+ Prisma schema loaded from prisma\schema.prisma
+ Datasource "db": SQLite database "dev.db" at "file:dev.db"
+
+ Applying migration `20231127041011_add_article_and_attachment`
+
+ The following migration(s) have been created and applied from new schema changes:
+
+ migrations/
+ └─ 20231127041011_add_article_and_attachment/
+ └─ migration.sql
+
+ Your database is now in sync with your schema.
+
+ ✔ Generated Prisma Client (v5.6.0) to .\node_modules\.pnpm\@prisma+client@5.6.0_prisma@5.6.0\node_modules\@prisma\client in 346ms
+
+** routes [#p0f2dd8e]
+
+- 投稿 (loggedIn)/articles/new
+- 一覧表示 /articles
+- 記事表示 /articles/[id]/[ [slug] ]
+- 編集 (loggedIn)/articles/[id]/edit
+- 削除 (loggedIn)/articles/[id]/delete
+
+** 投稿画面 [#t7c8104d]
+
+新たに LabeledTextArea を作成した。
+
+その中で editorProps というのを新しく作って、TextArea にそのまま渡すようにした。
+
+https://stackoverflow.com/questions/76285794/svelte-components-intrinsicelements-or-sveltehtmlelements-for-extending-with-ht
+
+src/lib/components/LabeledTextArea.svelte
+ LANG: ts
+ <script lang="ts">
+ import type { Writable } from 'svelte/store';
+ import type { SvelteHTMLElements } from 'svelte/elements';
+
+ export let name: string;
+ export let label: string;
+ export let type = 'text';
+ export let value: string;
+ export let disabled = false;
+ export let errors: Writable<{}> | undefined = undefined;
+
+ export let editorProps: SvelteHTMLElements['textarea'] | undefined = undefined;;
+
+ let key = name as keyof typeof errors;
+ </script>
+
+ <label for={name} class="label"><span class="text-base label-text">{label}</span></label>
+ <textarea {...{ name, type, ...editorProps }} bind:value disabled={disabled} on:input
+ class="w-full input input-bordered input-primary" class:input-error={errors && $errors && $errors[key]} />
+ {#if errors && $errors && $errors[key]}<span class="text-xs text-red-600">{$errors[key]}</span>{/if}
+
+後で見た目をもっと凝ったものにしたいけど今はこれで。
+
+(loggedIn)/articles/new/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { superForm } from 'sveltekit-superforms/client';
+ import Form from '$lib/components/Form.svelte';
+ import LabeledInput from '$lib/components/LabeledInput.svelte';
+ import LabeledTextArea from '$lib/components/LabeledTextArea.svelte';
+ import SubmitButton from '$lib/components/SubmitButton.svelte';
+
+ export let data: PageData;
+ const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {
+ taintedMessage: false
+ });
+ export const snapshot = { capture, restore };
+ </script>
+
+ <div><h1>記事の投稿</h1>
+ <Form message={$message} {enhance}>
+ <LabeledInput name="title" label="タイトル" bind:value={$form.title} disabled={$submitting} {errors} />
+
+ <LabeledTextArea name="body" label="本文" bind:value={$form.body} disabled={$submitting} {errors}
+ editorProps={{style: 'height: 20em; overflow-y: scroll'}}/>
+
+ <SubmitButton disabled={$submitting}>記事を投稿</SubmitButton>
+ <div>
+ <a class="text-sm text-blue-600 hover:underline hover:text-blue-400" href="/">トップへ戻る</a>
+ </div>
+ </Form>
+ </div>
+
+タイトルと本文と作者を設定してレコードを作成する。
+
+(loggedIn)/articles/new/+page.server.ts
+ LANG: ts
+ import type { Actions, PageServerLoad } from './$types';
+ import { schema } from '$lib/formSchemas/article';
+ import { superValidate } from 'sveltekit-superforms/server';
+ import { fail } from '@sveltejs/kit';
+ import { redirect } from 'sveltekit-flash-message/server';
+
+ import { db } from '$lib/server/db';
+
+ export const load = (async () => {
+ const form = await superValidate(schema);
+ return { form };
+ }) satisfies PageServerLoad;
+
+ export const actions: Actions = {
+ default: async (event) => {
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
+ if (!form.valid) {
+ return fail(400, { form });
+ }
+
+ // 記事を投稿する
+ const article = await db.article.create({data:{
+ authorId: event.locals.session.user.id,
+ ...form.data,
+ }})
+
+ return redirect(302, `/articles/${article.id}`, {type: 'success', message: '記事を投稿しました'}, event);
+ }
+ };
+
+
+** 記事の表示 [#e2289487]
+
+src/routes/articles/+page.server.ts
+ LANG: ts
+ import type { PageServerLoad } from './$types';
+
+ import { db } from '$lib/server/db';
+
+ export const load = (async () => {
+ const articles = await db.article.findMany({
+ where: {deletedAt: null, newRevisionId: null},
+ orderBy: {createdAt: 'desc'},
+ take: 20,
+ include: {author: true}
+ })
+ return { articles };
+ }) satisfies PageServerLoad;
+
+src/routes/articles/+page.svelte
+ <script lang="ts">
+ import type { PageData } from './$types';
+
+ export let data: PageData;
+ </script>
+
+ <div><h1>最新の記事</h1>
+ {#each data.articles as article}
+ <div>
+ <div>{article.title}</div>
+ <div><span>{article.author.name}</span><span>{article.createdAt.toString()}</span></div>
+ <div>
+ {article.body}
+ </div>
+ </div>
+ {/each}
+ </div>
+
+** markdown を解釈 [#tb240725]
+
+さしあたり marked, highlight, katex を入れてみる。
+
+ LANG: console
+ $ npm install marked highlight.js katex
+ Packages: +4
+ ++++
+ Progress: resolved 388, reused 362, downloaded 4, added 4, done
+
+ dependencies:
+ + highlight.js ^11.9.0
+ + katex ^0.16.9
+ + marked ^10.0.0
+
+ Done in 3s
+ $ npm i --save-dev @types/katex
+ Packages: +1
+ +
+ Progress: resolved 389, reused 366, downloaded 1, added 1, done
+
+ devDependencies:
+ + @types/katex 0.16.7
+
+ Done in 9.1s
+
+
+- https://note.com/taatn0te/n/n17db53037ade
+- https://katex.org/docs/node
+
+src/routes/articles/+page.svelte
+ ...
+
+ + import { marked } from 'marked';
+ + import hljs from 'highlight.js';
+ + import renderMathInElement from 'katex/contrib/auto-render';
+
+ export let data: PageData;
+ let articles: HTMLElement;
+
+ + afterUpdate(()=>{
+ + console.log('rendering...', articles);
+ + renderMathInElement(articles, {
+ + // customised options
+ + // • auto-render specific keys, e.g.:
+ + delimiters: [
+ + {left: '$$', right: '$$', display: true},
+ + {left: '$', right: '$', display: false},
+ + {left: '\\(', right: '\\)', display: false},
+ + {left: '\\[', right: '\\]', display: true}
+ + ],
+ + // • rendering keys, e.g.:
+ + throwOnError : false
+ + });
+ + hljs.highlightAll();
+ + })
+
+ </script>
+ ...
+
+ - {article.body}
+ + {@html marked.parse(article.body)}
+
+** css を読み込む [#h51651b5]
+
+node_modules に含まれるものは、
+
+src/routes/articles/+page.svelte
+ <script>
+ + import 'katex/dist/katex.min.css';
+ + import 'highlight.js/styles/arduino-light.min.css';
+
+のように import すればいいらしい。
+
+tailwind を使うときは、
+
+src/routes/articles/+page.svelte
+ + <style lang="postcss">
+ + article :global(h1) {
+ + @apply text-3xl font-bold;
+ + }
+ + </style>
+
+みたいに style に lang="postcss" をつける。
+
+** Artile を切り出す [#y03cda36]
+
+highlight.js によるマークアップが結構大変なのでそれも切り出す。
+
+src/lib/components/Article.svelte
+ LANG: html
+ <script lang="ts">
+ import { afterUpdate } from 'svelte';
+ import { marked } from 'marked';
+ import renderMathInElement from 'katex/contrib/auto-render';
+ import { codeRenderer } from '$lib/markedCodeRenderer';
+ import 'katex/dist/katex.min.css';
+ import 'highlight.js/styles/arduino-light.min.css';
+
+ import type { Article, AuthUser } from '@prisma/client'
+ export let article: Article & {author: AuthUser};
+
+ let element: HTMLElement;
+ afterUpdate(()=>{
+ renderMathInElement(element, {
+ // customised options
+ // • auto-render specific keys, e.g.:
+ delimiters: [
+ {left: '$$', right: '$$', display: true},
+ {left: '$', right: '$', display: false},
+ {left: '\\(', right: '\\)', display: false},
+ {left: '\\[', right: '\\]', display: true}
+ ],
+ // • rendering keys, e.g.:
+ throwOnError : false,
+ });
+ })
+
+ // code ブロックの処理を追加
+ const renderer = new marked.Renderer();
+ renderer.code = codeRenderer;
+ const originalHeadingRenderer = renderer.heading;
+ renderer.heading = (text: string, level: number, raw: string) =>
+ originalHeadingRenderer(text, level + 1, raw); // 1つレベルを下げる
+ marked.setOptions({ renderer });
+
+ function convert(body: string) {
+ return marked.parse(body);
+ }
+
+ </script>
+
+ <article class="article" bind:this={ element }>
+ <h1>{article.title}</h1>
+ <div><span>{article.author.name}</span> <span>{article.createdAt.toLocaleString()}</span></div>
+ <div>
+ {@html convert(article.body)}
+ </div>
+ </article>
+
+ <style lang="postcss">
+ article :global(h1) {
+ @apply text-2xl font-bold;
+ }
+ article :global(h2) {
+ @apply text-xl font-bold;
+ }
+ article :global(h3) {
+ @apply text-lg font-bold;
+ }
+ article :global(ul) {
+ @apply list-disc;
+ margin-left: 2em;
+ }
+ article :global(ol) {
+ @apply list-decimal;
+ margin-left: 2em;
+ }
+ </style>
+
+src/lib/markedCodeRenderer.ts
+ LANG: ts
+ import hljs from 'highlight.js';
+
+ // code を言語 lang として highlight する
+ const highlight = function(code: string, lang?: string){
+ if (lang) {
+ return hljs.highlight(code, {language: lang, ignoreIllegals: true}).value;
+ } else {
+ return hljs.highlightAuto(code).value;
+ }
+ }
+
+ // highlight.js の言語名あるいはエイリアス名から正式な言語名を調べる
+
+ const aliasToLang: {[alias: string]: string} = {};
+ hljs.listLanguages().forEach(name=>{
+ const lang = hljs.getLanguage(name);
+ aliasToLang[name] = name;
+ lang?.aliases?.forEach(alias=>{
+ aliasToLang[alias] = name;
+ })
+ });
+
+ // HTML の特殊文字をエスケープ
+ // https://stackoverflow.com/questions/1787322/what-is-the-htmlspecialchars-equivalent-in-javascript
+ function escapeHtml(text: string) {
+ const map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
+ return text.replace(/[&<>"']/g, c=>map[c as keyof typeof map]);
+ }
+
+ // code ブロックをレンダリングする
+ const codeRenderer = (code: string, fileInfo = '', escaped: boolean) => {
+ const info = fileInfo.split(':');
+ if(info[0]) {
+ if(!aliasToLang[info[0]]) {
+ const ext = info[0].split('.').findLast(()=>true);
+ if(ext && aliasToLang[ext]){
+ // 拡張子
+ info.unshift(aliasToLang[ext]);
+ } else {
+ // 言語指定なし
+ info.unshift('');
+ }
+ } else {
+ info[0] = aliasToLang[info[0]];
+ }
+ }
+ const [lang, fileName] = [...info, '', ''];
+
+ const out = highlight(code, lang);
+ if (out != null && out !== code) {
+ escaped = true;
+ code = out;
+ }
+
+ const fileTag = fileName ? `<span class="filename">${escapeHtml(fileName)}</span>` : '';
+ const langClass = lang ? ` language-${escapeHtml(lang)}` : '';
+
+ return `<pre>${fileTag}<code class="hljs${langClass}">`
+ + (escaped === false ? escapeHtml(code) : code)
+ + '\n</code></pre>';
+ };
+
+ export { codeRenderer };
+
+使う方はとてもすっきりする
+
+src/routes/articles/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import Article from '$lib/components/Article.svelte';
+ export let data: PageData;
+ </script>
+
+ <div><h1>最新の記事</h1>
+ <div class="articles">
+ {#each data.articles as article}
+ <Article { article } />
+ {/each}
+ </div>
+ </div>
+
+
+** highlight.js より prismjs というのが良いのかも? [#k4065edf]
+
+最近は prismjs の方が人気がありそうかも?~
+https://npmtrends.com/highlight.js-vs-prismjs
+
+特に diff 表示ができるのはうれしい:~
+https://qiita.com/suin/items/0303f5f121d836061bc8
+
+svelte には対応していない~
+https://prismjs.com/index.html#supported-languages
+
+まあ diff 表示は自分でやっても良いんだよね・・・
+
+
+** style の :global について [#xfd2fae0]
+
+svelte の style 定義には自動的にコンポーネントインスタンスの id が追加されるため、
+そのコンポーネントに直接含まれる(子コンポーネントは除外)エレメントにしか効果を及ぼさない。
+
+コンポーネントを超えて効果を期待する場合には :global を付ける。
+
+https://svelte.jp/docs/svelte-components#style
+ LANG: html
+ <style>
+ :global(strong) {
+ /* これはすべての <strong> に適用されます */
+ margin: 0;
+ }
+
+ div :global(strong) {
+ /* これは「このコンポーネント内の <div> 要素」の中にある
+ 「すべての <strong> 要素」に
+ 適用されます */
+ color: goldenrod;
+ }
+ </style>
+
+上記の例では div に :global がかかっていないため、div にはコンポーネントインスタンスの
+ID が付き、その下にあることが条件になっている。
+
+「このコンポーネントの子供」に適用したい場合にはこのテクニックが役に立ちそう。