-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathwatermark.js
65 lines (56 loc) · 1.93 KB
/
watermark.js
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
const WATERMARK_IMAGE = 'https://cml.dev/watermark.png';
class Watermark {
constructor(opts = {}) {
const { label = '', workflow, run, sha = false } = opts;
// Replace {workflow} and {run} placeholders in label with actual values.
this.label = label.replace('{workflow}', workflow).replace('{run}', run);
this.sha = sha;
}
// Appends the watermark (in markdown) to the report.
appendTo(report) {
return `${report}\n\n${this.toString()}`;
}
// Returns whether the watermark is present in the specified text.
// When checking for presence, the commit sha in the watermark is ignored.
isIn(text) {
const title = escapeRegExp(this.title);
const url = escapeRegExp(this.url({ sha: false }));
const pattern = `!\\[\\]\\(${url}#[0-9a-fA-F]+ "${title}"\\)`;
const re = new RegExp(pattern);
return re.test(text);
}
// String representation of the watermark.
toString() {
// Replace {workflow} and {run} placeholders in label with actual values.
return `} "${this.title}")`;
}
get title() {
let title = `CML watermark ${this.label}`.trim();
// Github appears to escape underscores and asterisks in markdown content.
// Without escaping them, the watermark content in comments retrieved
// from github will not match the input.
const patterns = [
[/_/g, '\\_'], // underscore
[/\*/g, '\\*'], // asterisk
[/\[/g, '\\['], // opening square bracket
[/</g, '\\<'] // opening angle bracket
];
title = patterns.reduce(
(label, pattern) => label.replace(pattern[0], pattern[1]),
title
);
return title;
}
url(opts = {}) {
const { sha = this.sha } = opts;
const watermarkUrl = new URL(WATERMARK_IMAGE);
if (sha) {
watermarkUrl.hash = sha;
}
return watermarkUrl.toString();
}
}
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
module.exports = { Watermark };