履歴: プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 6 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2023/12/5 12:56:30 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 最新版 | 27,067 | +129 | -0 | 表示 | 表示中 | — |
| 2023/11/30 14:16:52 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 | 23,526 | +362 | -218 | 表示 | 差分 | 選択 |
| 2023/11/30 5:49:32 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 | 18,110 | +207 | -41 | 表示 | 差分 | 選択 |
| 2023/11/30 0:36:00 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 | 14,091 | +153 | -123 | 表示 | 差分 | 基準 |
| 2023/11/28 10:21:21 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 | 13,513 | +1 | -1 | 表示 | 差分 | 選択 |
| 2023/11/28 10:21:12 | プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 | 13,512 | — | — | 表示 | — | 選択 |
基準: プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 (2023/11/30 0:36:00) → 比較: プログラミング/svelte/ブログ的なもの作り-投稿・表示・編集 (2023/12/5 12:56:30) 最新版
@@ -4,7 +4,7 @@
#contents
-** データベースの準備 [#ua1c2e03]
+* データベースの準備 [#ua1c2e03]
編集履歴をすべて残すため、記事が書き換えられた場合にも新しいレコードを追加するのみ。
@@ -82,7 +82,7 @@
- 編集 /articles/[id]/(loggedIn)/edit
- 削除 /articles/[id]/(loggedIn)/delete
-** 投稿画面 [#t7c8104d]
+* 投稿画面 [#t7c8104d]
新たに TextArea を作成した。
@@ -122,6 +122,7 @@
{/if}
</div>
+
後で見た目をもっと凝ったものにしたいけど今はこれで。
src/routes/articles/(login)/new/+page.svelte
@@ -136,17 +137,30 @@
export let data: PageData;
const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {
- taintedMessage: false
+ taintedMessage: false,
});
export const snapshot = { capture, restore };
</script>
- <div><h1>記事の投稿</h1>
+ <div>
+ <h1>記事の投稿</h1>
<Form message={$message} {enhance}>
- <InputText name="title" label="タイトル" bind:value={$form.title} disabled={$submitting} {errors} />
+ <InputText
+ name="title"
+ label="タイトル"
+ bind:value={$form.title}
+ disabled={$submitting}
+ {errors}
+ />
- <TextArea name="body" label="本文" bind:value={$form.body} disabled={$submitting} {errors}
- props={{style: 'height: 30em; overflow-y: scroll'}}/>
+ <TextArea
+ name="body"
+ label="本文"
+ bind:value={$form.body}
+ disabled={$submitting}
+ {errors}
+ props={{ style: 'height: 30em; overflow-y: scroll' }}
+ />
<Button disabled={$submitting}>記事を投稿</Button>
<div>
@@ -162,10 +176,10 @@
export const schema = z.object({
title: z.string().regex(/[^0-9]/, {
- message: 'タイトルが空あるいは数値のページは作成できません'
+ message: 'タイトルが空あるいは数値のページは作成できません',
}),
body: z.string(),
- })
+ });
タイトルと本文と作者を設定してレコードを作成する。
@@ -176,7 +190,7 @@
import { superValidate } from 'sveltekit-superforms/server';
import { fail, redirect } from '@sveltejs/kit';
import { setFlash } from 'sveltekit-flash-message/server';
- import { path } from '$lib/server';
+ import { path, addErrorToForm } from '$lib/server';
import { db } from '$lib/server/db';
export const load = (async () => {
@@ -208,8 +222,135 @@
},
} satisfies Actions;
-** 記事の表示 [#e2289487]
+ LANG: console
+ $ git add . && git commit -m "Article の投稿を可能にした"
+* 記事表示用のアドレス [#e2289487]
+
+実は上のコードの最後は path(article) という呼び出しを行っていて、
+これがまだ実装されていない。
+
+個々の記事を表示するためのアドレスはこの形にしたい。
+
+ /articles/[...titleOrId]
+
+[...titleOrId] には記事番号あるいは encodeURI されたタイトルが入る。
+
+スプレッド構文的に見えるため params.titleOrId は文字列配列になるのかと思ったけど、
+実際に渡されるのは "/" を含む可能性のある「文字列」だった。
+
+params.titleOrId が
+
++ 数字だけを含むなら記事番号(id)と解釈する
++ それ以外ならタイトルと解釈する
+
+2. を正式なアドレスとしたいので、数字でアクセスした場合にも
+タイトル表記のアドレスへリダイレクトする
+
+記事のタイトルが変更された場合に古いタイトルでアクセスしてきたなら
+最新のページにリダイレクトする
+
+** タイトルのエンコード・デコード [#q52d5736]
+
+さて、タイトルには特殊文字が含まれるのでそのまま URL としては使えない
+
+英語が主に使われている場所では slug 化してアドレスに直すことが多いのだけれど、
+今の用途では title は日本語であることが多いのでそのまま encodeURI してしまおう。
+
+- encodeURI
+-- UTF8ベースのエンコード
+-- その名の通り、URIそのものを渡しエンコードしてもらうもの
+-- そのため、URI内で意味をもつ #$&+,/:;=?@ の文字は変換されない
+-- スペースのエンコードは+ではなく%20
+- encodeURIComponent
+-- UTF-8ベースのエンコード
+-- その名の通り、URIを構成する一部分を渡しエンコードしてもらうもの
+-- URI内で意味を持つ #$&+,/:;=?@ もエンコードする
+-- encodeURIとの差は #$&+,/:;=?@ をエンコードするか否か
+-- URI全体をこの関数でエンコードすると、URIとして機能しなくなる
+
+encodeURI だけだと ?&# などパスに含まれちゃいけない文字がそのまま残るので、
+"/" でスプリットして個別に encodeURIComponent/decode するのが正しい。
+
+一方、そのまま encodeURIComponent を使うと
+スペースが "%20" になってしまって不格好なので、
+最後に "%20" を "+" に変換する。
+
+元々の % は %25 にエンコードされるので、何も考えず "%20" を "+" に置き換えて構わない。
+
+ LANG: ts
+ title.split('/')
+ .map(str=> encodeURIComponent(str).replaceAll('%20', '+'))
+ .join('/')
+
+戻すときは "+" を "%20" にしてから decodeURIComponent する。
+
+ LANG: ts
+ encoded.split('/')
+ .map(str=> decodeURIComponent(str.replaceAll('+', '%20')))
+ .join('/')
+
+** ユーティリティ関数 [#y859bb59]
+
+以上を実現するのに使うユーティリティ関数を次のように実装した。
+
+- articleTitleEncode(title) エンコードする
+- articleTitleDecode(encoded) デコードする
+- newestArticle(article) 最新版を探して返す
+
+src/lib/server/db.ts
+ LANG: ts
+ import { PrismaClient, type Article, type User } from '@prisma/client';
+ ...
+
+ articleTitleEncode(title: string) {
+ return title.split('/')
+ .map(str=> encodeURIComponent(str).replaceAll('%20', '+'))
+ .join('/');
+ }
+
+ articleTitleDecode(encoded: string) {
+ return encoded.split('/')
+ .map(str=> decodeURIComponent(str.replaceAll('+', '%20')))
+ .join('/');
+ }
+
+ async newestArticle(article: (Article & { author: User }) | number | null) {
+ if (!article) { return null; }
+ while (typeof article == 'number' || article?.newRevisionId) {
+ article = await this.article.findUnique({
+ where: {
+ id: typeof article == 'number' ? article : article.newRevisionId!,
+ deletedAt: null,
+ },
+ include: { author: true },
+ });
+ }
+ return article;
+ }
+
+これを使って path 関数に Article を指定可能にした。
+
+src/lib/server/index.ts
+ LANG: ts
+ import type { Article } from '@prisma/client';
+ import { db } from '$lib/server/db';
+
+ export const urlRoot = process.env['URL_ROOT'] || '';
+ export function path(relative: string | Article) {
+ if (typeof relative == 'string') {
+ return urlRoot + relative;
+ } else {
+ // if (Object.hasOwn(relative, 'newRevisionId')) {
+ return urlRoot + '/articles/' + db.articleTitleEncode(relative.title);
+ }
+ }
+
+ LANG: console
+ $ git add . && git commit -m "Article 記事の URL 生成ヘルパー"
+
+* 記事の簡易表示 [#e2289487]
+
src/routes/articles/[...titleOrId]/+page.server.ts
LANG: ts
import type { PageServerLoad } from './$types';
@@ -246,8 +387,13 @@
return { article };
}) satisfies PageServerLoad;
-とりあえず <pre></pre> にソーステキストをそのまま貼ってみる。
+表示はとりあえず <pre></pre> にソーステキストをそのまま貼っておく
+tailwindcss はそのまま使うと何もかもがリセットされていてアレなのだけれど、
+tailwindcss/typography を入れてあるので class="prose" で囲った領域は
+まともな表示(例えば h1 が大きくなるなど)になる。ただし maxWidth が 65ch
+に設定されてしまうため min-w-full でキャンセルしておく。
+
src/routes/articles/[...titleOrId]/+page.svelte
LANG: html
<script lang="ts">
@@ -256,270 +402,563 @@
export let data: PageData;
</script>
- <article class="prose p-4">
+ <article class="prose min-w-full p-4">
<h1>{data.article.title}</h1>
<pre>
{data.article.body}
</pre>
</article>
-** markdown を解釈 [#tb240725]
+ LANG: console
+ $ git add . && git commit -m "Article の簡易表示ページを作成"
-さしあたり marked, highlight, katex を入れてみる。
+* markdown を解釈 [#tb240725]
- LANG: console
- $ npm install marked highlight.js katex
- Packages: +4
- ++++
- Progress: resolved 383, reused 359, downloaded 1, 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 384, reused 361, downloaded 0, added 1, done
-
- devDependencies:
- + @types/katex 0.16.7
-
- Done in 9.1s
+記事は markedown で書いて [[marked>https://marked.js.org/]] で html に直して表示する。
+https://marked.js.org/using_advanced#extensions
-- https://note.com/taatn0te/n/n17db53037ade
-- https://katex.org/docs/node
+を見るといろいろ Extention があって、それらを入れるだけで
+highlight や katex も処理できるみたい。
-src/routes/articles/+page.svelte
- ...
-
- + import { afterUpdate } from 'svelte';
- + import { marked } from 'marked';
- + import hljs from 'highlight.js';
- + import renderMathInElement from 'katex/contrib/auto-render';
-
- export let data: PageData;
- + let article: 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]
+- [[marked-base-url>https://github.com/markedjs/marked-base-url]] 相対リンクの base を指定
+- [[marked-linkify-it>https://www.npmjs.com/package/marked-linkify-it]] リンクっぽいところにリンクを張る
+- [[marked-highlight>https://www.npmjs.com/package/marked-highlight]] コードブロックのハイライト
+- [[marked-katex-extension>https://www.npmjs.com/package/marked-katex-extension]] Katex で数式を処理
+- [[marked-alert>https://www.npmjs.com/package/marked-alert]] 警告付き引用のようなものを表示する
+- [[marked-custom-heading-id>https://www.npmjs.com/package/marked-custom-heading-id]] 見出しのアンカーを設定(pukiwiki からの移行に使えそう)
+-- [[marked-gfm-heading-id]] と両方入れられるのか?
+- [[marked-directive>https://www.npmjs.com/package/marked-directive]] プラグイン的なのを実現する
+- [[marked-emoji>https://www.npmjs.com/package/marked-emoji]] 絵文字
+- [[marked-extended-tables>https://www.npmjs.com/package/marked-extended-tables]] テーブルの拡張
+- [[marked-footnote>https://www.npmjs.com/package/marked-footnote]] 脚注
-node_modules に含まれるものは、
+気になるのはいっぱいあるけど、さしあたり base-url, linkify-it, highlight, katex を入れてみる。
-src/routes/articles/+page.svelte
- <script>
- + import 'katex/dist/katex.min.css';
- + import 'highlight.js/styles/arduino-light.min.css';
+ LANG: console
+ $ pnpm add marked marked-base-url marked-linkify-it marked-highlight marked-katex-extension
+ Packages: +11
+ +++++++++++
+ Progress: resolved 390, reused 360, downloaded 7, added 7, done
+
+ dependencies:
+ + marked 11.0.0
+ + marked-base-url 1.1.2
+ + marked-highlight 2.0.8
+ + marked-katex-extension 4.0.5
+ + marked-linkify-it 3.1.7
+
+ WARN Issues with peer dependencies found
+ .
+ └─┬ marked-linkify-it 3.1.7
+ └── ✕ unmet peer marked@">=4 <11": found 11.0.0
+
+ Done in 4.5s
-のように import すればいいらしい。
+linkify-it が marked 11 には対応していないと言っている。
-tailwind を使うときは、
+> https://github.com/UziTech/marked-linkify-it/commit/08019477c049181e14e9c83bc40171270b4c46dc
+> ここに v11 を許すようにしたというコミットがあった。
-src/routes/articles/+page.svelte
- + <style lang="postcss">
- + article :global(h1) {
- + @apply text-3xl font-bold;
- + }
- + </style>
+不具合があれば marked のバージョンを落とそう。
-みたいに style に lang="postcss" をつける。
+別途 highlight.js を入れる必要があった。
-** Artile を切り出す [#y03cda36]
+src/app.html に1行追加は katex のため:
+ LANG: html
+ + <link rel="stylesheet"
+ + href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css"
+ + integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV"
+ + crossorigin="anonymous">
-highlight.js によるマークアップが結構大変なのでそれも切り出す。
+content="module" で marked の初期化をしたら、
-src/lib/components/Article.svelte
+ {@html marked.parse(data.article.body)}
+
+だけで変換が完了する。
+
+src/routes/articles/[...titleOrId]/+page.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';
+ <script lang="ts" context="module">
+ import { marked } from "marked";
+ import { baseUrl } from "marked-base-url";
+ import markedLinkifyIt from "marked-linkify-it";
+ import { markedHighlight } from "marked-highlight";
+ import hljs from 'highlight.js';
+ import markedKatex from "marked-katex-extension";
- import type { Article, AuthUser } from '@prisma/client'
- export let article: Article & {author: AuthUser};
+ import 'highlight.js/styles/stackoverflow-dark.min.css';
- 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,
- });
- })
+ marked.use(baseUrl("https://example.com/folder/"));
+ const linkifyItSchemas = {};
+ const linkifyItOptions = {};
+ marked.use(markedLinkifyIt(linkifyItSchemas, linkifyItOptions));
+ marked.use(markedHighlight({
+ langPrefix: 'hljs language-',
+ highlight(code, lang) {
+ const language = hljs.getLanguage(lang) ? lang : 'plaintext';
+ return hljs.highlight(code, { language }).value;
+ }
+ }));
+ marked.use(markedKatex({
+ throwOnError: false
+ }));
+ </script>
- // 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 lang="ts">
+ import type { PageData } from './$types';
+ export let data: PageData;
</script>
- <article class="article" bind:this={ element }>
- <h1>{article.title}</h1>
- <div><span>{article.author.name}</span> <span>{article.createdAt.toLocaleString()}</span></div>
+ <article class="prose max-w-full p-4">
+ <h1>{data.article.title}</h1>
<div>
- {@html convert(article.body)}
+ <span>{data.article.author.name}</span>
+ <span>{data.article.createdAt.toLocaleString()}</span>
</div>
+ <content>
+ {@html marked.parse(data.article.body)}
+ </content>
</article>
+
+marked-highlight は言語指定がちゃんとしてないと働かないようで、
+ちょっと思ったのと違いそう?
+
+後で調整することにして、とりあえず編集機能を先に実装しよう。
+
+ LANG: console
+ $ git add . && git commit -m "Article の表示用に marked を入れた"
+
+* 編集機能を実装 [#p0274804]
+
+ログインユーザーのみが
+
+ /articles/[...titleOrId]/edit
+
+というアドレスで編集できるようにする。
+
+表示ページに編集へのリンクを付ける:~
+現在の URL を取得するには import { page } from '$app/stores'; を使う
+
+src/routes/articles/[...titleOrId]/+page.svelte
+ LANG: html
+ + import { page } from '$app/stores';
+ ...
+
+ <article class="prose max-w-full p-4">
+ <h1>{data.article.title}</h1>
+ <div>
+ <span>{data.article.author.name}</span>
+ <span>{data.article.createdAt.toLocaleString()}</span>
+ + <span><a href={$page.url + '/edit'}>編集</a></span>
+
+
+ページタイトルの末尾に /edit などの動詞が含まれるとダメなので排除する。
+
+src/lib/zod/articles/new.ts
+ LANG: ts
+ title: z.string().regex(/[^0-9]/, {
+ message: 'タイトルが空あるいは数値のページは作成できません',
+ + }).regex(/(?<!\/(edit|rename|delete)$)/, {
+ + message: 'タイトルが /edit, /rename, /delete で終わるページは作成できません'
+ }),
+
+titleOrId から Article を探す部分を切り出す
+
+src/routes/articles/[...titleOrId]/articleFromTitleOrId.ts
+ LANG: ts
+ import { error } from '@sveltejs/kit';
+ import { db } from '$lib/server/db';
- <style lang="postcss">
- article :global(h1) {
- @apply text-2xl font-bold;
+ export async function articleFromTitleOrId(titleOrId: string) {
+ // 数値で指定なら該当記事にリダイレクト
+ if (titleOrId.match(/^\d+$/)) {
+ const article = await db.newestArticle(Number(titleOrId));
+ if (!article) throw error(404);
+ return { article, needRedirect: true };
}
- 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>
+ // タイトルで指定
+ const article = await db.article.findFirst({
+ where: {
+ deletedAt: null,
+ title: decodeURI(titleOrId),
+ },
+ orderBy: {
+ createdAt: 'desc',
+ },
+ include: { author: true },
+ });
+ if (!article) throw error(404);
+
+ // 最新版があればリダイレクト
+ const newest = await db.newestArticle(article);
+ return { article, needRedirect: newest?.id != article.id };
+ }
-src/lib/markedCodeRenderer.ts
+src/routes/articles/[...titleOrId]/+page.server.ts
LANG: ts
- import hljs from 'highlight.js';
+ import type { PageServerLoad } from './$types';
+ import { redirect } from '@sveltejs/kit';
+ import { path } from '$lib/server';
+ * import { articleFromTitleOrId } from './articleFromTitleOrId';
- // 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;
+ export const load = (async (event) => {
+ * const { article, needRedirect } =
+ * await articleFromTitleOrId(event.params.titleOrId);
+ * if(needRedirect) {
+ * throw redirect(302, path(article));
+ * }
+ return { article };
+ }) satisfies PageServerLoad;
+
+これと articles/new/+page.server.ts からコピペしてちょちょっと直す。
+
+新しいページを投稿してその値を newRevisionId に入れるには、
+普通ならトランザクションを使うのだけれど Prisma では
+[[nested-write を使うのが筋みたい。>https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#create-a-related-record]]
+
+ただ、なぜか思った通りの動作にならない
+
+ LANG: ts
+ const oldArticle = await db.article.update({
+ where: { id: article.id },
+ data: {
+ newRevision: {
+ create: {
+ authorId: event.locals.session!.user.userId,
+ ...form.data,
+ },
+ },
+ },
+ include: { newRevision: true },
+ });
+
+これで article.id の記事の newRevision に新しい記事のが入ることを期待したのだけれど、
+逆に新しい記事の newRevision に古い記事が入ってしまう。わけわからん。たぶんバグ???
+
+include がまずいのかと思って外してみたけど変わらない。
+
+仕方がないのでアトミックじゃない書き方で我慢した。
+
+src/routes/articles/[...titleOrId]/(login)/edit/+page.server.ts
+ LANG: ts
+ import type { Actions, PageServerLoad } from './$types';
+ import { schema } from '$lib/zod/articles/new';
+ import { superValidate } from 'sveltekit-superforms/server';
+ import { fail, redirect } from '@sveltejs/kit';
+ import { setFlash } from 'sveltekit-flash-message/server';
+ import { path, addErrorToForm } from '$lib/server';
+ import { db } from '$lib/server/db';
+ import { articleFromTitleOrId } from '../../articleFromTitleOrId';
+
+ export const load = (async (event) => {
+ // article を取り出して、必要なら最新版のページへ飛ぶ
+ const { article, needRedirect } = await articleFromTitleOrId(event.params.titleOrId);
+ if (needRedirect) {
+ throw redirect(302, path(article) + '/edit');
}
- };
- // highlight.js の言語名あるいはエイリアス名から正式な言語名を調べる
+ const form = await superValidate(schema);
+ form.data.title = article.title;
+ form.data.body = article.body;
+ return { form };
+ }) satisfies PageServerLoad;
- const aliasToLang: { [alias: string]: string } = {};
- hljs.listLanguages().forEach((name) => {
- const lang = hljs.getLanguage(name);
- aliasToLang[name] = name;
- lang?.aliases?.forEach((alias) => {
- aliasToLang[alias] = name;
- });
- });
+ export const actions = {
+ default: async (event) => {
+ // フォームデータのバリデーション
+ const form = await superValidate(event, schema);
- // 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]);
- }
+ const { article, needRedirect } = await articleFromTitleOrId(event.params.titleOrId);
+ if (needRedirect) {
+ form.message = '編集が衝突しました。最新版のページからやり直してください。';
+ form.valid = false;
+ }
- // 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 pageWithSameTitle = await db.article.findFirst({
+ where: { title: form.data.title, newRevisionId: null, deletedAt: null },
+ orderBy: { createdAt: 'desc' },
+ });
+ if (pageWithSameTitle && pageWithSameTitle.id != article.id) {
+ addErrorToForm(form, 'title', '既存のページと重複しています');
}
- }
- const [lang, fileName] = [...info, '', ''];
- const out = highlight(code, lang);
- if (out != null && out !== code) {
- escaped = true;
- code = out;
- }
+ if (!form.valid) {
+ return fail(400, { form });
+ }
- const fileTag = fileName ? `<span class="filename">${escapeHtml(fileName)}</span>` : '';
- const langClass = lang ? ` language-${escapeHtml(lang)}` : '';
+ if (form.data.title == article.title && form.data.body == article.body) {
+ setFlash({type: 'error', message: '何も変更されませんでした'}, event);
+ throw redirect(302, event.url.toString().replace(/\/edit$/, ''));
+ }
- return (
- `<pre>${fileTag}<code class="hljs${langClass}">` +
- (escaped === false ? escapeHtml(code) : code) +
- '\n</code></pre>'
- );
- };
+ // 記事を投稿して古い記事の newRevision に入れる
+ // const newArticle = await db.article.create({
+ // data: {
+ // authorId: event.locals.session!.user.userId,
+ // oldRevision: article,
+ // ...form.data,
+ // }
+ // });
+ // oldRevision には代入できないみたいだ
- export { codeRenderer };
+ // これで良さそうなのだけれどなぜか新しくできた方の
+ // newRevision に古い方が入ってしまう。えー。
+ //
+ // const oldArticle = await db.article.update({
+ // where: { id: article.id },
+ // data: {
+ // newRevision: {
+ // create: {
+ // authorId: event.locals.session!.user.userId,
+ // ...form.data,
+ // },
+ // },
+ // },
+ // include: { newRevision: true },
+ // });
+
+ // 仕方がないのでデータの整合性が失われる可能性のある書き方で我慢
+
+ const newArticle = await db.article.create({
+ data: {
+ authorId: event.locals.session!.user.userId,
+ ...form.data,
+ }
+ });
+
+ await db.article.update({
+ where: { id: article.id },
+ data: { newRevisionId: newArticle.id },
+ });
+
+ setFlash({ type: 'success', message: '編集を反映しました' }, event);
+ throw redirect(302, path(newArticle));
+ },
+ } satisfies Actions;
-使う方はとてもすっきりする
+新規投稿フォームから ArticleEditor を切り出す。
-src/routes/articles/+page.svelte
+src/routes/articles/ArticleEditor.svelte
+ LANG: ts
+ <script lang="ts">
+ import type { Writable } from 'svelte/store';
+ import Form from '$lib/components/Form.svelte';
+ import InputText from '$lib/components/InputText.svelte';
+ import TextArea from '$lib/components/TextArea.svelte';
+ import Button from '$lib/components/Button.svelte';
+
+ export let message = '';
+ export let enhance: (el: HTMLFormElement) => object = () => {
+ return {};
+ };
+ export let title: string;
+ export let body: string;
+ export let disabled: boolean;
+ export let errors: Writable<{}> | undefined = undefined;
+ </script>
+
+ <div>
+ <h1>記事の編集</h1>
+ <Form message={message} {enhance}>
+ <InputText
+ name="title"
+ label="タイトル"
+ bind:value={title}
+ disabled={disabled}
+ {errors}
+ />
+
+ <TextArea
+ name="body"
+ label="本文"
+ bind:value={body}
+ disabled={disabled}
+ {errors}
+ props={{ style: 'height: 30em; overflow-y: scroll' }}
+ />
+
+ <Button disabled={disabled}>記事に反映</Button>
+ <div>
+ <!-- svelte-ignore a11y-invalid-attribute -->
+ <a class="link" href="javascript:history.back()">戻る</a>
+ </div>
+ </Form>
+ </div>
+
+src/routes/articles/(login)/new/+page.svelte
LANG: html
<script lang="ts">
import type { PageData } from './$types';
- import Article from '$lib/components/Article.svelte';
+ import { superForm } from 'sveltekit-superforms/client';
+ import ArticleEditor from '../../ArticleEditor.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>
- <div class="articles">
- {#each data.articles as article}
- <Article { article } />
- {/each}
- </div>
- </div>
+ <ArticleEditor
+ formTitle="記事の投稿" submitTitle="記事を投稿"
+ {...{ message: $message, enhance, disabled: $submitting, errors }}
+ bind:title={$form.title} bind:body={$form.body}
+ />
+src/routes/articles/[...titleOrId]/(login)/edit/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { superForm } from 'sveltekit-superforms/client';
+ import ArticleEditor from '../../../ArticleEditor.svelte';
+
+ export let data: PageData;
+ const { form, message, errors, submitting, capture, restore, enhance } = superForm(data.form, {
+ taintedMessage: false,
+ });
+ export const snapshot = { capture, restore };
+ </script>
+
+ <ArticleEditor
+ formTitle="記事を編集" submitTitle="編集を反映"
+ {...{ message: $message, enhance, disabled: $submitting, errors }}
+ bind:title={$form.title} bind:body={$form.body}
+ />
-** highlight.js より prismjs というのが良いのかも? [#k4065edf]
+編集できるようになった。
-最近は prismjs の方が人気がありそうかも?~
-https://npmtrends.com/highlight.js-vs-prismjs
+ LANG: console
+ $ git add . && git commit -m "Article の編集機能を実装した"
-特に diff 表示ができるのはうれしい:~
-https://qiita.com/suin/items/0303f5f121d836061bc8
+* marked-directive も必要だ [#fe35b1dc]
-svelte には対応していない~
-https://prismjs.com/index.html#supported-languages
+pukiwiki の plugin を移植すると使いやすそう。
-まあ diff 表示は自分でやっても良いんだよね・・・
+ LANG: console
+ $ npm i marked-directive
+ Packages: +4
+ ++++
+ Progress: resolved 394, reused 367, downloaded 4, added 4, done
+
+ dependencies:
+ + marked-directive 1.0.3
+
+ LANG: ts
+ import { createDirectives, presetDirectiveConfigs, type DirectiveConfig } from 'marked-directive';
+ ...
+ marked.use(
+ createDirectives([
+ ...presetDirectiveConfigs,
+ {
+ level: 'inline',
+ marker: ':',
+ renderer(token) {
+ if (token.meta.name === 'ruby') {
+ return (
+ `<ruby>${token.text}<rp> (</rp>` +
+ `<rt>${token.attrs?.getTokens()}</rt><rp>)</rp></ruby>`
+ );
+ }
+ }
+ }
+ ])
+ )
+
+みたいに書ける。
+
+* marked-katex-extension だけだと痒いところに手が届かなかった [#bb57be9a]
+
+手作業での変換を減らすためにかなり細かく調整したくなる。
+
+普通の $ と数式の
+$ とをどのように見分けるかがキモ。
+
+まだいろいろ調整中
+
+ LANG: ts
+ const katexRenderer = (text: string, displayMode: boolean) => {
+ return katex.renderToString(text, {
+ displayMode,
+ throwOnError: false,
+ fleqn: true,
+ strict: 'warn',
+ macros: {
+ '\\hbar': 'h\\mathllap{{}^-}',
+ '\\set': '\\left\\{#1\\right\\}',
+ '\\setm': '\\left\\{#1\\ \\middle\\vert\\ #2\\right\\}',
+ '\\bra': '\\left\\langle#1\\right|',
+ '\\ket': '\\left|#1\\right\\rangle',
+ '\\braket': '\\left\\langle#1\\middle|#2\\right\\rangle',
+ '\\braketm': '\\left\\langle#1\\middle|#2\\middle|#3\\right\\rangle',
+ '\\mathrm': '\\text{#1}',
+ '\\DIV': '\\,\\text{div}\\,',
+ '\\grad': '\\,\\text{grad}\\,',
+ '\\rot': '\\,\\text{rot}\\,',
+ '\\curl': '\\,\\text{curl}\\,',
+ '\\PD': '\\partial',
+ '\\ne': '\\,\\mathrlap{\\,/}{=}\\,',
+ '\\MARU': '\\hbox{\\textcircled{\\scriptsize{#1}}}',
+ },
+ });
+ };
+
+ marked.use({
+ extensions: [
+ {
+ name: 'blockKatex',
+ level: 'block',
+ tokenizer(src) {
+ const match = src.match(/^\$\$((?:[^$]|\$(?!\$)|\n)+?)\$\$\s*(?=\n|$)/);
+ if (match) {
+ return {
+ type: 'blockKatex',
+ raw: match[0],
+ text: match[1].trim(),
+ };
+ }
+ },
+ renderer: (token) => {
+ // < > のエスケープを外す
+ return katexRenderer(token.text.replaceAll(/\\(?=[<>])/g, ''), true);
+ },
+ },
+ {
+ name: 'inlineKatex',
+ level: 'inline',
+ start(src) {
+ let index;
+ let indexSrc = src;
+
+ while (indexSrc) {
+ index = indexSrc.indexOf('$');
+ if (index < 0) return;
+
+ if (index === 0 || indexSrc[index - 1].match(/^[\s\n::,、,(()。「・]$/)) {
+ const possibleKatex = indexSrc.substring(index);
+ if (possibleKatex.match(/^\$[^$]+?\$(?=[\s,、,)) 」。((・~\|]|\n|$)/)) {
+ return index;
+ }
+ }
+ indexSrc = indexSrc.substring(index + 1).replace(/^\$+/, '');
+ }
+ },
+ tokenizer(src) {
+ const match = src.match(/^(\${1,2})(.+?)\1/);
+ if (match) {
+ return {
+ type: 'inlineKatex',
+ raw: match[0],
+ text: match[2].trim(),
+ };
+ }
+ },
+ renderer: (token) => {
+ // < > のエスケープを外す
+ return katexRenderer(token.text.replaceAll(/\\(?=[<>])/g, ''), false);
+ },
+ },
+ ],
+ });