返回 slidev
utils.test.ts
根目录 / packages / slidev / node / utils.test.ts
1 import { describe, expect, it } from 'vitest'
2 import { applyNotesAutoRuby, isPathInsideRoots } from './utils'
3
4 describe('isPathInsideRoots', () => {
5 it('accepts a path nested inside a root', () => {
6 expect(isPathInsideRoots('/a/b/c', ['/a'])).toBe(true)
7 })
8
9 it('accepts a path that equals the root itself', () => {
10 expect(isPathInsideRoots('/a', ['/a'])).toBe(true)
11 })
12
13 it('rejects a path outside every root', () => {
14 expect(isPathInsideRoots('/x', ['/a'])).toBe(false)
15 })
16
17 it('rejects a `..`-escaping relative resolution', () => {
18 expect(isPathInsideRoots('/a/../x', ['/a'])).toBe(false)
19 })
20
21 it('accepts when any of several roots contains the path', () => {
22 expect(isPathInsideRoots('/b/c', ['/a', '/b'])).toBe(true)
23 })
24 })
25
26 describe('applyNotesAutoRuby', () => {
27 it('wraps a matched key in a ruby tag', () => {
28 expect(applyNotesAutoRuby('私は勉強しています。', { 勉強: 'べんきょう' }))
29 .toBe('私は<ruby>勉強<rt>べんきょう</rt></ruby>しています。')
30 })
31
32 it('prefers the longest key when one key starts with another', () => {
33 expect(applyNotesAutoRuby('私は日本語を話す', { 日本: 'ni hon', 日本語: 'ni hon go' }))
34 .toBe('私は<ruby>日本語<rt>ni hon go</rt></ruby>を話す')
35 })
36
37 it('matches keys containing regular expression characters literally', () => {
38 expect(applyNotesAutoRuby('I write C++ code', { 'C++': 'see plus plus' }))
39 .toBe('I write <ruby>C++<rt>see plus plus</rt></ruby> code')
40 expect(applyNotesAutoRuby('the (foo) case', { '(foo)': 'ふー' }))
41 .toBe('the <ruby>(foo)<rt>ふー</rt></ruby> case')
42 })
43
44 it('only matches whole words for alphanumeric keys', () => {
45 expect(applyNotesAutoRuby('slidev slidevjs', { slidev: 'slide dev' }))
46 .toBe('<ruby>slidev<rt>slide dev</rt></ruby> slidevjs')
47 })
48
49 it('returns the input untouched when there is no key', () => {
50 expect(applyNotesAutoRuby('unchanged', {})).toBe('unchanged')
51 })
52 })
53
53 lines TYPESCRIPT