-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshare.js
More file actions
35 lines (32 loc) · 1.04 KB
/
share.js
File metadata and controls
35 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const isPlainObject = (value) => {
if (!value || typeof value !== 'object') { return false }
return Object.getPrototypeOf(value) === Object.prototype
}
export const parseSharePayload = (hash) => {
if (!hash || typeof hash !== 'string') { return null }
if (!hash.startsWith('share=')) { return null }
try {
const raw = decodeURIComponent(hash.slice(6))
const data = JSON.parse(raw)
if (!isPlainObject(data)) { return null }
return data
} catch (err) {
return null
}
}
const normalizeText = (value) => {
if (typeof value !== 'string') { return '' }
return value.trim()
}
export const buildShareMessage = (payload) => {
if (!payload || typeof payload !== 'object') { return '' }
const text = normalizeText(payload.text)
const url = normalizeText(payload.url)
const title = normalizeText(payload.title)
const linkLabel = title || url
const link = url ? `[${linkLabel}](${url})` : ''
if (!link) { return text }
if (!text) { return link }
if (text.includes(url)) { return text }
return `${text}\n\n${link}`
}