-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcopybutton_funcs.js
47 lines (41 loc) · 1.49 KB
/
copybutton_funcs.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
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
// Callback when a copy button is clicked. Will be passed the node that was clicked
// should then grab the text and replace pieces of text that shouldn't be used in output
export function formatCopyText(textContent, copybuttonPromptText, isRegexp = false, onlyCopyPromptLines = true, removePrompts = true) {
var regexp;
var match;
// create regexp to capture prompt and remaining line
if (isRegexp) {
regexp = new RegExp('^(' + copybuttonPromptText + ')(.*)')
} else {
regexp = new RegExp('^(' + escapeRegExp(copybuttonPromptText) + ')(.*)')
}
const outputLines = [];
var promptFound = false;
for (const line of textContent.split('\n')) {
match = line.match(regexp)
if (match) {
promptFound = true
if (removePrompts) {
outputLines.push(match[2])
} else {
outputLines.push(line)
}
} else {
if (!onlyCopyPromptLines) {
outputLines.push(line)
}
}
}
// If no lines with the prompt were found then just use original lines
if (promptFound) {
textContent = outputLines.join('\n');
}
// Remove a trailing newline to avoid auto-running when pasting
if (textContent.endsWith("\n")) {
textContent = textContent.slice(0, -1)
}
return textContent
}