履歴: プログラミング/svelte/pukiwikiの記事を流し込む
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 7 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2023/12/4 20:41:22 | プログラミング/svelte/pukiwikiの記事を流し込む 最新版 | 19,170 | +22 | -8 | 表示 | 差分 | — |
| 2023/12/4 4:15:58 | プログラミング/svelte/pukiwikiの記事を流し込む | 19,007 | +75 | -0 | 表示 | 表示中 | 選択 |
| 2023/12/3 13:00:31 | プログラミング/svelte/pukiwikiの記事を流し込む | 17,146 | +36 | -10 | 表示 | 差分 | 選択 |
| 2023/12/3 3:41:46 | プログラミング/svelte/pukiwikiの記事を流し込む | 16,229 | +100 | -0 | 表示 | 差分 | 選択 |
| 2023/12/2 13:31:57 | プログラミング/svelte/pukiwikiの記事を流し込む | 13,503 | +164 | -0 | 表示 | 差分 | 選択 |
| 2023/12/2 4:40:52 | プログラミング/svelte/pukiwikiの記事を流し込む | 9,430 | +293 | -3 | 表示 | 差分 | 基準 |
| 2023/12/2 1:26:17 | プログラミング/svelte/pukiwikiの記事を流し込む | 1,725 | — | — | 表示 | — | 選択 |
基準: プログラミング/svelte/pukiwikiの記事を流し込む (2023/12/2 4:40:52) → 比較: プログラミング/svelte/pukiwikiの記事を流し込む (2023/12/4 4:15:58)
@@ -341,6 +341,296 @@
modified: prisma/schema.prisma
new file: prisma/seedPukiwikiData.ts
+* Recent ページの作成 [#o084aafd]
+
+流し込んだ記事を確認する用途にも、最近更新された記事を確認できるページが欲しい。
+
+src/routes/articles/recent/+page.svelte
+ LANG: ts
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import { path } from '../lib';
+ export let data: PageData;
+ </script>
+
+ <div class="prose">
+ <ul>
+ {#each data.articles as article}
+ <li>
+ <a href={path(article)}>{article.title}</a>
+ <small>({article.createdAt.toLocaleString()})</small>
+ </li>
+ {/each}
+ </ul>
+ </div>
+
+ <style>
+ li {
+ margin-top: 0;
+ margin-bottom: 0;
+ }
+ </style>
+
+src/routes/articles/recent/+page.svelte
+
+* 最新記事の一覧 [#d13b4623]
+
+変換したページを確認するにも、記事一覧がないと話にならない。
+
+最終編集時刻順に表示するページを作る。
+
+で、まず必要になるのが Pagination コントロール
+
+DaisyUI を使うと表示側は難しくなく書ける。
+
+src/lib/components/Pagination.svelte
+ LANG: html
+ <script lang="ts">
+ import { createEventDispatcher } from 'svelte';
+
+ export let pages: number;
+ export let page: number;
+
+ let pageNums = [] as number[];
+ for(let i = 0; i < pages; i++) {
+ pageNums[i] = i + 1;
+ }
+ const dispatch = createEventDispatcher();
+
+ function click(num: number) {
+ dispatch('click', {num});
+ }
+ </script>
+
+ <div class="join">
+ <button class="join-item btn{page==1?' btn-disabled':''}" on:click={()=>click(page-1)}> Prev </button>
+ {#each pageNums as num}
+ {#if num == 1 || (num <= 7 && page <= 4) || Math.abs(num - page) <= 2 || (pages - 4 <= page && pages - 6 <= num) || num == pages }
+ <button class="join-item btn{page==num?' btn-active':''}" on:click={()=>click(num)}>{num}</button>
+ {:else}
+ {#if num == 2 || num == pages - 1}
+ <button class="join-item btn btn-disabled">...</button>
+ {/if}
+ {/if}
+ {/each}
+ <button class="join-item btn{page==pages?' btn-disabled':''}" on:click={()=>click(page+1)}> Next </button>
+ </div>
+
+on:click の e.detail.page に選択されたページ番号が入る。
+
+で、最新記事一覧はこのコンポーネントを使って、
+
+src/routes/articles/recent/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import Pagination from '$lib/components/Pagination.svelte';
+ import type { PageData } from './$types';
+ import { path } from '../lib';
+ export let data: PageData; // { article, page, pages, size }
+
+ function click(e: CustomEvent) {
+ location.search = `page=${e.detail.page}`;
+ }
+ </script>
+
+ <div class="prose">
+ <Pagination page={data.page} pages={data.pages} on:click={click} />
+ <ul>
+ {#each data.articles as article}
+ <li>
+ <a href={path(article)}>{article.title}</a>
+ <small>({article.createdAt.toLocaleString()})</small>
+ </li>
+ {/each}
+ </ul>
+ <Pagination page={data.page} pages={data.pages} on:click={click} />
+ </div>
+
+ <style>
+ li {
+ margin-top: 0;
+ margin-bottom: 0;
+ }
+ </style>
+
+サーバー側はこう。
+
+src/routes/articles/recent/+page.server.ts
+ LANG: ts
+ import type { PageServerLoad } from './$types';
+ import { db } from '$lib/server/db';
+
+ export const load = (async (event) => {
+ let page = Number(event.url.searchParams.get('page')) || 1;
+ let size = Number(event.url.searchParams.get('size')) || 50;
+ if(size < 10) size = 10;
+ if(page < 0) page = 0;
+
+ const condition = {
+ where: {
+ newRevisionId: null,
+ deletedAt: null,
+ },
+ };
+
+ const count = await db.article.count(condition);
+
+ const pages = Math.ceil(count/size);
+ if(page > pages) page = pages;
+
+ const articles = await db.article.findMany({
+ ...condition,
+ orderBy: {
+ createdAt: 'desc',
+ },
+ skip: (page - 1) * size,
+ take: size,
+ select: {
+ title: true,
+ createdAt: true,
+ author: true,
+ },
+ });
+ return { articles, page, pages, size };
+ }) satisfies PageServerLoad;
+
+これで表示できるようになった。
+
+ LANG: console
+ $ git add . && git commit -m "articles/recent ページを作成"
+ new file: src/lib/components/Pagination.svelte
+ new file: src/routes/articles/recent/+page.server.ts
+ new file: src/routes/articles/recent/+page.svelte
+
+本当は +page.server.ts じゃなく +page.ts で fetch を使って表示できるように
+api を整えたほうが良いんだろうけど・・・そこらへんはまた後で調べる。
+
+* 編集時に syntax も変更できるようにする [#m1a9cb4d]
+
+新規作成時も選べる。
+
+src/lib/components/Select.svelte
+ <script lang="ts">
+ import type { Writable } from 'svelte/store';
+
+ export let name: string;
+ export let label: string;
+ export let labelAlt: string = '';
+ export let value: string;
+ export let disabled = false;
+ export let errors: Writable<{}> | undefined = undefined;
+ export let props: svelteHTML.IntrinsicElements['select'] | undefined = undefined;
+
+ let key = name as keyof typeof errors;
+ </script>
+
+ <div class="form-control w-full">
+ <label for={name} class="label">
+ <span class="label-text">{label}</span>
+ {#if labelAlt}<span class="label-text-alt">{labelAlt}</span>{/if}
+ </label>
+ <select
+ {...{ name, ...props }}
+ bind:value
+ {disabled}
+ class="w-full select select-bordered select-primary"
+ class:input-error={errors && $errors && $errors[key]}
+ >
+ <slot />
+ </select>
+ {#if errors && $errors && $errors[key]}
+ <label class="label" for={name}>
+ <span class="label-text-alt text-error">{$errors[key]}</span>
+ </label>
+ {/if}
+ </div>
+
+src/routes/articles/zod-schema.ts
+ LANG ts
+ import { z } from 'zod';
+
+ export const schema = z.object({
+ title: z
+ .string()
+ .regex(/[^0-9]/, {
+ message: 'タイトルが空あるいは数値のページは作成できません',
+ })
+ .regex(/(?<!\/(edit|rename|delete)$)/, {
+ message: 'タイトルが /edit, /rename, /delete で終わるページは作成できません',
+ }),
+ body: z.string(),
+ + syntax: z.number().min(0).max(1),
+ });
+
+src/routes/articles/ArticleEditor.svelte
+ LANG: html
+ import Select from '$lib/components/Select.svelte';
+ ...
+
+ export let syntax: number;
+
+ function syntaxChange(e: Event) {
+ const select = e.target as HTMLSelectElement;
+ syntax=Number(select.options[select.selectedIndex].value)
+ }
+ ...
+
+ <Select name="syntax" label="文法" {disabled} {errors}
+ value={syntax.toString()} on:change={syntaxChange}>
+ <option value="0">Markdown</option>
+ <option value="1">Pukiwiki</option>
+ </Select>
+
+数値フィールドを select に結び付けるのに直接 bind:value できないので、
+value と on:change で双方向バインディングを実現している。
+
+src/routes/articles/[...titleOrId]/(login)/edit/+page.svelte
+ <ArticleEditor
+ formTitle="記事を編集"
+ submitTitle="編集を反映"
+ {...{ message: $message, enhance, disabled: $submitting, errors }}
+ bind:title={$form.title}
+ bind:body={$form.body}
+ + bind:syntax={$form.syntax}
+ />
+
+src/lib/index.ts
+ LANG: ts
+ export function contractTo<T extends object, S extends T>(larger: S, smaller: T) {
+ const result={} as T;
+ Object.getOwnPropertyNames(smaller).forEach(k=> {
+ if(Object.hasOwn(smaller, k)) {
+ result[k as keyof T] = larger[k as keyof T];
+ }
+ });
+ return result;
+ }
+
+src/routes/articles/[...titleOrId]/(login)/edit/+page.server.ts
+ ...
+
+ + import { contractTo } from '$lib';
+ ...
+
+ - form.data.title = article.title;
+ - form.data.body = article.body;
+ + form.data = contractTo(article, form.data);
+ ...
+
+ + if (form.data.title == article.title && form.data.body == article.body) {
+ - if (form.data == contractTo(article, form.data)) {
+
+
+ LANG: console
+ $ git commit -m "Syntax を編集できるようにした"
+ new file: src/lib/components/Select.svelte
+ modified: src/lib/index.ts
+ modified: src/routes/articles/(login)/new/+page.svelte
+ modified: src/routes/articles/ArticleEditor.svelte
+ modified: src/routes/articles/[...titleOrId]/(login)/edit/+page.server.ts
+ modified: src/routes/articles/[...titleOrId]/(login)/edit/+page.svelte
+ modified: src/routes/articles/zod-schema.ts
+
* pukiwiki から markdown への変換 [#r3517e0a]
pukiwiki → markdown → html の変換がうまく行くようになれば、~
@@ -349,3 +639,78 @@
pukiwiki の文法は実はかなりややこしい → [[整形ルール]]
しかも掟破りの複数行処理プラグインを入れてたりするのでさらにややこしい。
+
+src/routes/articles/[...titleOrId]/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import type { PageData } from './$types';
+ import ArticleElement from '$lib/components/ArticleElement.svelte';
+ import { path } from '../lib';
+ + import { pukiwikiToMarkdown } from '$lib/pukiwikiToMarkdown';
+ + import { onMount } from 'svelte';
+
+ export let data: PageData;
+ +
+ + let body = '';
+ + onMount(() => {
+ + body = data.article.syntax == 1 ? pukiwikiToMarkdown(data.article.body) : data.article.body;
+ + });
+ </script>
+
+ <ArticleElement
+ title={data.article.title}
+ * body={body}
+ author={data.article.author.name}
+ date={data.article.createdAt}
+ permLink={path(data.article)}
+ />
+
+としておいて、pukiwikiToMarkdown の完成度を徐々に高める方針。
+
+** テストページを作る [#f3819817]
+
+部分的にテストがしやすいように
+入力した pukiwiki 文法を markdown 文法に直して見せる
+テストページを作っておく
+
+src/routes/pukiwikiToMarkdown/+page.svelte
+ LANG: html
+ <script lang="ts">
+ import TextArea from '$lib/components/TextArea.svelte';
+ import { pukiwikiToMarkdown } from '$lib/pukiwikiToMarkdown';
+ import type { Snapshot } from '@sveltejs/kit';
+
+ let psrc = '';
+ let msrc = '';
+
+ export const snapshot: Snapshot<string> = {
+ capture: () => psrc,
+ restore: (value) => (psrc = value),
+ }
+
+ function convert(e: Event): void {
+ msrc = pukiwikiToMarkdown(psrc);
+ }
+ </script>
+
+ <div>
+ <TextArea name="pukiwiki" label="Pukiwikiソース" bind:value={psrc} on:input={convert} />
+ <TextArea name="markdown" label="Markdownソース" value={msrc} props={{ readonly: true }} />
+ </div>
+
+ <style lang="postcss">
+ div {
+ display: grid;
+ grid-template-columns: 1fr 1fr;
+ grid-template-rows: auto;
+ }
+ div :global(textarea) {
+ font-family: monospace;
+ font-size: smaller;
+ min-height: 22lh;
+ line-height: 1lh;
+ }
+ </style>
+
+** 変換処理 [#f3676cab]
+