94 行
3.3 KiB
TypeScript
94 行
3.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
|
||
import remarkWikiAutolink from '@/lib/remark-wiki-autolink'
|
||
|
||
import type { Root } from 'mdast'
|
||
|
||
import type { WikiAutolinkCandidate } from '@/types'
|
||
|
||
|
||
const candidate = (
|
||
name: string,
|
||
resourceId: number,
|
||
overrides: Partial<WikiAutolinkCandidate> = {},
|
||
): WikiAutolinkCandidate => ({
|
||
name, resourceId, tagId: resourceId + 100, tagDescriptionId: resourceId,
|
||
resourceType: 'tag_description', languageCode: 'ja', localeCode: 'ain-Kana',
|
||
...overrides })
|
||
|
||
|
||
const paragraph = (value: string): Root => ({
|
||
type: 'root', children: [{
|
||
type: 'paragraph', children: [{ type: 'text', value }] }] })
|
||
|
||
|
||
describe ('remarkWikiAutolink', () => {
|
||
it ('prefers longer names and keeps the viewing locale for Japanese text', () => {
|
||
const tree = paragraph ('虹夏 and 虹')
|
||
|
||
remarkWikiAutolink ([candidate ('虹', 1), candidate ('虹夏', 2)],
|
||
'ja', 'ain-Kana') (tree)
|
||
|
||
expect (tree.children[0]).toMatchObject ({
|
||
children: [
|
||
{ type: 'link', url: '/ain-Kana/wiki/2',
|
||
children: [{ type: 'text', value: '虹夏' }] },
|
||
{ type: 'text', value: ' and ' },
|
||
{ type: 'link', url: '/ain-Kana/wiki/1',
|
||
children: [{ type: 'text', value: '虹' }] }] })
|
||
})
|
||
|
||
it ('does not rewrite existing links, code or images', () => {
|
||
const tree: Root = {
|
||
type: 'root', children: [
|
||
{ type: 'paragraph', children: [
|
||
{ type: 'link', url: '/existing', children: [{ type: 'text', value: '虹' }] },
|
||
{ type: 'inlineCode', value: '虹' },
|
||
{ type: 'image', url: '/image', alt: '虹' }] },
|
||
{ type: 'code', value: '虹' }] }
|
||
const original = structuredClone (tree)
|
||
|
||
remarkWikiAutolink ([candidate ('虹', 1)], 'ja', 'ain-Kana') (tree)
|
||
|
||
expect (tree).toEqual (original)
|
||
})
|
||
|
||
it ('links an alias to its description rather than its tag ID or matched name', () => {
|
||
const tree = paragraph ('別名')
|
||
remarkWikiAutolink ([candidate ('別名', 9, { tagId: 123 })],
|
||
'ja', 'ain-Kana') (tree)
|
||
expect (tree.children[0]).toMatchObject ({
|
||
children: [{ type: 'link', url: '/ain-Kana/wiki/9' }] })
|
||
})
|
||
|
||
it ('leaves ambiguous names and other locales or languages unlinked', () => {
|
||
const tree = paragraph ('虹 名前 name')
|
||
const original = structuredClone (tree)
|
||
remarkWikiAutolink ([candidate ('虹', 1), candidate ('虹', 2),
|
||
candidate ('名前', 3, { localeCode: 'ja' }),
|
||
candidate ('name', 4, { languageCode: 'ain' })], 'ja', 'ain-Kana') (tree)
|
||
expect (tree).toEqual (original)
|
||
})
|
||
|
||
it ('links Ainu words only at word boundaries', () => {
|
||
const tree = paragraph ('チャペ チャペ。チャペア')
|
||
remarkWikiAutolink ([candidate ('チャペ', 5, { languageCode: 'ain' })],
|
||
'ain', 'ain-Kana') (tree)
|
||
expect (tree.children[0]).toMatchObject ({
|
||
children: [
|
||
{ type: 'link', url: '/ain-Kana/wiki/5' },
|
||
{ type: 'text', value: ' ' },
|
||
{ type: 'link', url: '/ain-Kana/wiki/5' },
|
||
{ type: 'text', value: '。チャペア' }] })
|
||
})
|
||
|
||
it ('preserves tag-group resource identity', () => {
|
||
const tree = paragraph ('group')
|
||
remarkWikiAutolink ([candidate ('group', 8, {
|
||
resourceType: 'tag_group', tagId: null, tagDescriptionId: null })],
|
||
'ja', 'ain-Kana') (tree)
|
||
expect (tree.children[0]).toMatchObject ({
|
||
children: [{ type: 'link', url: '/ain-Kana/wiki/8' }] })
|
||
})
|
||
})
|