Repository navigation
Expand file tree
/
Copy pathplugin.js
More file actions
89 lines (72 loc) · 2.17 KB
/
Copy pathplugin.js
File metadata and controls
89 lines (72 loc) · 2.17 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
function makeHeadingLinesUppercase() {
for (const line of Beat.lines()) {
if (line.type === Beat.type.heading) {
Beat.replaceRange(line.position, line.length, line.string.toUpperCase());
}
}
}
function removeExtraEmptyLines() {
let emptyCount = 0;
const toRemove = [];
const lines = Beat.lines();
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
if (line.typeAsString() === "Empty") {
emptyCount++;
if (emptyCount > 1) {
toRemove.push({
position: line.position,
length: line.string.length + 1,
});
}
} else {
emptyCount = 0;
}
}
for (const range of toRemove) {
Beat.replaceRange(range.position, range.length, "");
}
return toRemove.length;
}
function ensureEmptyLineAfterHeadings() {
const lines = Beat.lines();
const toInsert = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
if (line.typeAsString() === "Heading") {
const prev = lines[i + 1];
if (prev.typeAsString() !== "Empty") {
toInsert.push(prev.position);
}
}
}
for (let i = toInsert.length - 1; i >= 0; i--) {
Beat.replaceRange(toInsert[i], 0, "\n");
}
return toInsert.length;
}
function removeLineBreaksInDialogue() {
const lines = Beat.lines();
const toReplace = [];
for (let i = 0; i < lines.length - 1; i++) {
const line = lines[i];
const next = lines[i + 1];
if (line.typeAsString() === "Dialogue" && next.typeAsString() === "Dialogue") {
// Replace the \n between the two dialogue lines with a space
toReplace.push(line.position + line.string.length);
}
}
for (let i = toReplace.length - 1; i >= 0; i--) {
Beat.replaceRange(toReplace[i], 1, " ");
}
return toReplace.length;
}
const removedLines = removeExtraEmptyLines();
const addedLines = ensureEmptyLineAfterHeadings();
const removedDialogueBreaks = removeLineBreaksInDialogue();
removeExtraEmptyLines();
makeHeadingLinesUppercase();
Beat.alert(
"Done",
`Removed ${removedLines} extra empty line(s), added ${addedLines} missing blank line(s) before scene headings, removed ${removedDialogueBreaks} line break(s) in dialogue.`,
);