@playpilot/tpi 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/link-injections.js +7 -7
- package/package.json +1 -1
- package/src/lib/api.js +3 -0
- package/src/lib/data/translations.js +238 -0
- package/src/lib/enums/Language.js +4 -0
- package/src/lib/localization.js +36 -0
- package/src/lib/meta.js +82 -0
- package/src/main.js +3 -1
- package/src/routes/+layout.svelte +2 -0
- package/src/routes/components/AfterArticlePlaylinks.svelte +6 -5
- package/src/routes/components/Editorial/Editor.svelte +1 -1
- package/src/routes/components/Genres.svelte +8 -3
- package/src/routes/components/Modal.svelte +1 -1
- package/src/routes/components/Playlinks.svelte +8 -7
- package/src/routes/components/Popover.svelte +1 -1
- package/src/routes/components/Title.svelte +3 -2
- package/src/tests/lib/api.test.js +5 -0
- package/src/tests/lib/localization.test.js +67 -0
- package/src/tests/lib/meta.test.js +201 -0
- package/src/typedefs.js +8 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest'
|
|
2
|
+
import { getPageHeading, getPageModifiedTime, getPagePublishedTime } from '$lib/meta'
|
|
3
|
+
|
|
4
|
+
describe('meta.js', () => {
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
document.body.innerHTML = ''
|
|
7
|
+
document.head.innerHTML = ''
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
describe('getPageHeading', () => {
|
|
11
|
+
it('Should get the h1 in the given parent', () => {
|
|
12
|
+
document.body.innerHTML = `
|
|
13
|
+
<h1>Not this heading</h1>
|
|
14
|
+
<section>
|
|
15
|
+
<h1>This heading</h1>
|
|
16
|
+
</section>
|
|
17
|
+
`
|
|
18
|
+
|
|
19
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
20
|
+
expect(getPageHeading(parent)).toBe('This heading')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('Should get the first h1 on the page if no h1 is found in the given parent', () => {
|
|
24
|
+
document.body.innerHTML = `
|
|
25
|
+
<h1>This heading</h1>
|
|
26
|
+
<section>
|
|
27
|
+
<h2>Not this heading</h2>
|
|
28
|
+
</section>
|
|
29
|
+
`
|
|
30
|
+
|
|
31
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
32
|
+
expect(getPageHeading(parent)).toBe('This heading')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('Should return null if heading is not found', () => {
|
|
36
|
+
document.body.innerHTML = `
|
|
37
|
+
<section>
|
|
38
|
+
<h2>Not this heading</h2>
|
|
39
|
+
</section>
|
|
40
|
+
`
|
|
41
|
+
|
|
42
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
43
|
+
expect(getPageHeading(parent)).toBe(null)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('Should return null if heading is empty', () => {
|
|
47
|
+
document.body.innerHTML = `
|
|
48
|
+
<section>
|
|
49
|
+
<h1> </h1>
|
|
50
|
+
</section>
|
|
51
|
+
`
|
|
52
|
+
|
|
53
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
54
|
+
expect(getPageHeading(parent)).toBe(null)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('getPageModifiedTime', () => {
|
|
59
|
+
it('Should use element containing itemprop when available, using the content attribute', () => {
|
|
60
|
+
document.body.innerHTML = `
|
|
61
|
+
<div itemprop="dateModified" content="2025-01-01">2025-01-02</div>
|
|
62
|
+
`
|
|
63
|
+
|
|
64
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('body'))
|
|
65
|
+
expect(getPageModifiedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('Should use element inside of given selector before others', () => {
|
|
69
|
+
document.body.innerHTML = `
|
|
70
|
+
<div itemprop="dateModified">2025-01-01</div>
|
|
71
|
+
<section>
|
|
72
|
+
<div itemprop="dateModified">2025-01-02</div>
|
|
73
|
+
</section>
|
|
74
|
+
`
|
|
75
|
+
|
|
76
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
77
|
+
expect(getPageModifiedTime(parent)).toBe('2025-01-02T00:00:00.000Z')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('Should use meta tag before other elements', () => {
|
|
81
|
+
document.head.innerHTML = '<meta content="2025-01-01" property="article:modified_time">'
|
|
82
|
+
document.body.innerHTML = `
|
|
83
|
+
<div itemprop="dateModified">2025-01-02</div>
|
|
84
|
+
<section>
|
|
85
|
+
<div itemprop="dateModified">2025-01-03</div>
|
|
86
|
+
</section>
|
|
87
|
+
`
|
|
88
|
+
|
|
89
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
90
|
+
expect(getPageModifiedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe('getPagePublishedTime', () => {
|
|
95
|
+
it('Should get the time element inside the parent', () => {
|
|
96
|
+
document.body.innerHTML = `
|
|
97
|
+
<time>2025-01-01</time>
|
|
98
|
+
<section>
|
|
99
|
+
<time>2025-01-02</time>
|
|
100
|
+
</section>
|
|
101
|
+
`
|
|
102
|
+
|
|
103
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
104
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-02T00:00:00.000Z')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('Should get the time element out of the parent when parent contains none', () => {
|
|
108
|
+
document.body.innerHTML = `
|
|
109
|
+
<time>2025-01-01</time>
|
|
110
|
+
<section>
|
|
111
|
+
</section>
|
|
112
|
+
`
|
|
113
|
+
|
|
114
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
115
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('Should get the time element inside the parent', () => {
|
|
119
|
+
document.body.innerHTML = `
|
|
120
|
+
<time>2025-01-01</time>
|
|
121
|
+
<section>
|
|
122
|
+
<time>2025-01-02</time>
|
|
123
|
+
</section>
|
|
124
|
+
`
|
|
125
|
+
|
|
126
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
127
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-02T00:00:00.000Z')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('Should get the time element out of the parent when parent contains none', () => {
|
|
131
|
+
document.body.innerHTML = `
|
|
132
|
+
<time>2025-01-01</time>
|
|
133
|
+
<section>
|
|
134
|
+
</section>
|
|
135
|
+
`
|
|
136
|
+
|
|
137
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
138
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
it('Should use element containing itemprop before others', () => {
|
|
142
|
+
document.body.innerHTML = `
|
|
143
|
+
<time>2025-01-02</time>
|
|
144
|
+
<div itemprop="datePublished">2025-01-02</div>
|
|
145
|
+
<section>
|
|
146
|
+
<time>2025-01-03</time>
|
|
147
|
+
</section>
|
|
148
|
+
`
|
|
149
|
+
|
|
150
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
151
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-02T00:00:00.000Z')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('Should use element containing datetime if neither time or datePublished are present', () => {
|
|
155
|
+
document.body.innerHTML = `
|
|
156
|
+
<section>
|
|
157
|
+
<div datetime="2025-01-01"></div>
|
|
158
|
+
</section>
|
|
159
|
+
`
|
|
160
|
+
|
|
161
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
162
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('Should return datetime attribute over text', () => {
|
|
166
|
+
document.body.innerHTML = `
|
|
167
|
+
<section>
|
|
168
|
+
<time datetime="2025-01-01">2025-01-02</time>
|
|
169
|
+
</section>
|
|
170
|
+
`
|
|
171
|
+
|
|
172
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
173
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('Should return innertext if no datetime attribute is given', () => {
|
|
177
|
+
document.body.innerHTML = `
|
|
178
|
+
<section>
|
|
179
|
+
<time>2025-01-01</time>
|
|
180
|
+
</section>
|
|
181
|
+
`
|
|
182
|
+
|
|
183
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
184
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('Should use meta tag before other elements', () => {
|
|
188
|
+
document.head.innerHTML = '<meta content="2025-01-01" property="article:published_time">'
|
|
189
|
+
document.body.innerHTML = `
|
|
190
|
+
<time>2025-01-02</time>
|
|
191
|
+
<div itemprop="datePublished">2025-01-03</div>
|
|
192
|
+
<section>
|
|
193
|
+
<time>2025-01-04</time>
|
|
194
|
+
</section>
|
|
195
|
+
`
|
|
196
|
+
|
|
197
|
+
const parent = /** @type {HTMLElement} */ (document.querySelector('section'))
|
|
198
|
+
expect(getPagePublishedTime(parent)).toBe('2025-01-01T00:00:00.000Z')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
})
|
package/src/typedefs.js
CHANGED
|
@@ -70,3 +70,11 @@
|
|
|
70
70
|
/**
|
|
71
71
|
* @typedef {{ x: number, y: number }} Position
|
|
72
72
|
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {{ heading: string | null, modified_time: string | null, published_time: string | null }} ArticleMetaData
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {'en-US' | 'sv-SE'} LanguageCode
|
|
80
|
+
*/
|