この
スラッグとはhttps://kkhys.me/posts/p1y4nft でp1y4nft と
気に
こだわりポイント
私が
- 自分で
決めなくて 良い - 美しい
文字列 - 打ち間違いを
しにくい
自分で 決めなくて 良い
コンテンツは
コンテンツを
私の2024-01-26 みたいな)を
この
.|-- life| `-- 2023-12-31| `-- index.mdx| `-- 01.jpg| `-- 02.jpg`-- tech |-- 2024-01-20 | `-- index.mdx `-- 2024-01-26 `-- index.mdxContentlayer では
export const Post = defineDocumentType(() => ({ ... computedFields: { slug: { description: 'Generate post slug from id', type: 'string', resolve: ({ _id }) => generateSlug(_id), }, }})関数 generateSlug は_id が
_id はposts/life/2024-01-26/index.mdx と
一意な
書き忘れていたが、particular-about-slug みたいに
美しい 文字列
さて、
先ほどgenerateSlug は
/** * Generates a unique slug based on the given data. * * @param data - The data used to generate the slug. * @returns The generated slug. */export const generateSlug = (data: crypto.BinaryLike) => { const hashAlgorithm = 'sha512'; const encoding = 'hex'; const slugLength = 7; const prefix = 'p';
const hashValue = crypto.createHash(hashAlgorithm).update(data).digest(encoding); const buffer = Buffer.from(hashValue, encoding); const words = bech32m.toWords(buffer);
return bech32m.encode(prefix, words, 1024).slice(0, slugLength);};
const id = 'posts/life/2024-01-26/index.mdx';
console.log(generateSlug(id)); // p1y4nftハッシュアルゴリズムにはSHA-512 をMD5
文字数は
打ち間違いを しにくい
ハッシュ関数を
https://kkhys.me/posts/0oO1IlL極端な
打ち間違いを
誤記を0, O, l, I)を
どんな
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyzこれで
- QR コード生成時に
英数字モードを 使えないため、 QR コードの サイズが 大きくなる - 大文字と
小文字が 混在している ため、 メモしたり、 モバイルキーボードで 打ち込んだり、 読み上げたりする ときに 不便
その
Bech32 では1b, i, o と
使われている
qpzry9x8gf2tvdw0s3jn54khce6mua7lBech32 は
かなり
まとめると
# id (Generated by Contentlayer)posts/life/2024-01-26/index.mdx
↓
# SHA-512256695b8af9372eb6484a04d906b5675ff56bf845116adcb2594ca2e7d834bd982e26c000161902ae259f7b509e12458843b1abaee8dae2061f8b6723d4ae301
↓
# Bech32m (prefix is "p")p1y4nftw90jdewkeyy5pxeq66kwhl4d0uy2yt2mje9jn9zulvrf0vc9cnvqqqkryp2ufvl0dgfuyj93ppmr2awardwypsl3dnj849wxqgr38kz4
↓
# Cut out the first 7 charactersp1y4nftBech32m のp とbc)。1 をposts のp とした。
最後に、
