Disclaimeer: I'm not really a Javascript developer and I've never used Vue.js or ProseMirror before. I'm pretty much just fiddling with things and seeing what happens.
Issue
When I copy an email with two-space-separated sentences from the Gmail web client and paste it into Replier, the sentences are not split (each paragraph is treated as a single sentence).
When I copy the same email text from a text editor or use the "paste as plain text" option on Windows (there is no such option on my Ubuntu machine) the sentences actually are split correctly. This has me quite confused!
Debugging
I used the following test email:
This is a test email! This is the second sentence? This is the third sentence.
Single space, This is a test email! This is the second sentence? This is the third sentence.
I then added some log statements to the selection.js
code:
/**
* Create a decorator for each sentence in the document
*/
function split(doc) {
const result = [];
// Text start position relative to current node
let start = 0;
function record(from, to, position, node) {
result.push({
from: from + position,
to: to + position,
text: node.text.substring(from, to)
});
}
// For each node in the document
doc.descendants((node, position) => {
// position: Node start position relative to whole document
start = 0;
if (node.isText) {
console.log("Splitting node with text:", node.text);
let match;
// Record blocks of text between each match of "EOSPuncRegex"
// eslint-disable-next-line no-cond-assign
console.log("End of sentence regex matches sentence:", /([.?!]) /.test(node.text));
while ((match = EOSPuncRegex.exec(node.text))) {
const end = match.index + 1;
record(start, end, position, node);
start = end;
// Ignore whitespace, find next character position
const nextCharacter = node.text
.substring(end)
.match(nonWhitespaceRegex);
if (nextCharacter) {
start += nextCharacter.index;
}
}
// Record remaining text untill end of node/line
record(start, node.text.length, position, node);
}
});
return result;
}
The key log statement being console.log("End of sentence regex matches sentence:", /([.?!]) /.test(node.text));
When I paste in plain text, I see the following console output:
- Splitting node with text: This is a test email! This is the second sentence? This is the third sentence.
- End of sentence regex matches sentence: true
- Splitting node with text: Single space, This is a test email! This is the second sentence? This is the third sentence.
- End of sentence regex matches sentence: true
When I paste copied directly from the Gmail client I see the following console output:
- Splitting node with text: This is a test email! This is the second sentence? This is the third sentence.
- End of sentence regex matches sentence: false
- Splitting node with text: Single space, This is a test email! This is the second sentence? This is the third sentence.
- End of sentence regex matches sentence: true
From what I can tell with fiddling with things, it seems the global /g
flag makes for some unexpected behavior. I'm thinking of trying to re-write the split
function to avoid using this and see if that addresses the matter. However, I was wondering if others had suggestions for a better approach.