-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Expand file tree
/
Copy pathoriginalityProof.js
More file actions
90 lines (77 loc) · 2.11 KB
/
Copy pathoriginalityProof.js
File metadata and controls
90 lines (77 loc) · 2.11 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { sha256 } from 'js-sha256'
const TRUE_VALUES = new Set(['1', 'true', 'yes', 'y', 'on', '是', '启用'])
function hasValue(value) {
return value !== undefined && value !== null && String(value).trim() !== ''
}
function hasProofMetadata(post = {}) {
return (
hasValue(post.proofHash) ||
hasValue(post.proofUrl) ||
hasValue(post.ext?.proofHash) ||
hasValue(post.ext?.proofUrl)
)
}
export function isOriginalityProofEnabled(globalEnabled, post = {}) {
const localFlag = post.proof ?? post.originalityProof ?? post.ext?.proof
if (localFlag !== undefined && localFlag !== null) {
return (
localFlag === true ||
TRUE_VALUES.has(String(localFlag).trim().toLowerCase())
)
}
return (
globalEnabled === true ||
globalEnabled === 'true' ||
hasProofMetadata(post)
)
}
function normalizeText(value) {
return String(value ?? '').replace(/\s+/g, ' ').trim()
}
function normalizeUrl(siteUrl, post = {}) {
const base = String(siteUrl || '').replace(/\/$/, '')
const path = post.href || post.slug || post.id || ''
if (/^https?:\/\//.test(path)) {
return path
}
return `${base}/${String(path).replace(/^\//, '')}`
}
export function createOriginalityProof({
enabled,
post,
content,
author,
siteUrl
}) {
if (!enabled || !post || !hasValue(content)) {
return null
}
const canonicalUrl = normalizeUrl(siteUrl, post)
const payload = {
title: normalizeText(post.title),
pageId: normalizeText(post.id),
author: normalizeText(author),
url: canonicalUrl,
content: normalizeText(content)
}
const localHash = sha256(JSON.stringify(payload))
const externalHash = post.proofHash || post.ext?.proofHash
const proofUrl = post.proofUrl || post.ext?.proofUrl || ''
const proofTime =
post.proofTime ||
post.ext?.proofTime ||
post.lastEditedDate ||
post.publishDate ||
''
return {
algorithm: 'SHA-256',
hash: externalHash || localHash,
localHash,
proofTime,
proofUrl,
pageId: post.id,
title: post.title,
url: canonicalUrl,
provider: externalHash || proofUrl ? 'external' : 'local'
}
}