mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-12 15:01:38 +03:00
tokeniseMessage: handle linebreaks correctly
This commit is contained in:
parent
2949701c7f
commit
f64d582a4e
@ -17,15 +17,15 @@ const isRef = (str) => {
|
||||
|
||||
const tokenizeMessage = (text) => {
|
||||
let messages = [];
|
||||
let message = [];
|
||||
// by line
|
||||
let currTextBlock = [];
|
||||
let isInCodeBlock = false;
|
||||
let endOfCodeBlock = false;
|
||||
text.split(/\r?\n/).forEach((line, index) => {
|
||||
if (index !== 0) {
|
||||
message.push('\n');
|
||||
}
|
||||
// by space
|
||||
let currTextLine = [];
|
||||
// A line of backticks enters and exits a codeblock
|
||||
if (line.startsWith('```')) {
|
||||
if (line.trim().startsWith('```')) {
|
||||
// But we need to check if we've ended a codeblock
|
||||
endOfCodeBlock = isInCodeBlock;
|
||||
isInCodeBlock = (!isInCodeBlock);
|
||||
@ -34,9 +34,11 @@ const tokenizeMessage = (text) => {
|
||||
}
|
||||
|
||||
if (isInCodeBlock || endOfCodeBlock) {
|
||||
message.push(line);
|
||||
currTextLine = [line];
|
||||
} else {
|
||||
line.split(/\s/).forEach((str) => {
|
||||
const words = line.split(/\s/);
|
||||
words.forEach((str, idx) => {
|
||||
const last = words.length - 1 === idx;
|
||||
if (
|
||||
(str.startsWith('`') && str !== '`')
|
||||
|| (str === '`' && !isInCodeBlock)
|
||||
@ -50,9 +52,12 @@ const tokenizeMessage = (text) => {
|
||||
}
|
||||
|
||||
if(isRef(str) && !isInCodeBlock) {
|
||||
if (message.length > 0) {
|
||||
if (currTextLine.length > 0 || currTextBlock.length > 0) {
|
||||
// If we're in the middle of a message, add it to the stack and reset
|
||||
messages.push({ text: message.join(' ') });
|
||||
currTextLine.push('');
|
||||
messages.push({ text: currTextBlock.join('\n') + currTextLine.join(' ') });
|
||||
currTextBlock = last ? [''] : [];
|
||||
currTextLine = [];
|
||||
}
|
||||
const link = parsePermalink(str);
|
||||
if(!link) {
|
||||
@ -61,34 +66,39 @@ const tokenizeMessage = (text) => {
|
||||
const reference = permalinkToReference(link);
|
||||
messages.push(reference);
|
||||
}
|
||||
message = [];
|
||||
currTextLine = [];
|
||||
} else if (isUrl(str) && !isInCodeBlock) {
|
||||
if (message.length > 0) {
|
||||
if (currTextLine.length > 0 || currTextBlock.length > 0) {
|
||||
// If we're in the middle of a message, add it to the stack and reset
|
||||
messages.push({ text: message.join(' ') });
|
||||
message = [];
|
||||
currTextLine.push('');
|
||||
messages.push({ text: currTextBlock.join('\n') + currTextLine.join(' ') });
|
||||
currTextBlock = last ? [''] : [];
|
||||
currTextLine = [];
|
||||
}
|
||||
messages.push({ url: str });
|
||||
message = [];
|
||||
currTextLine = [];
|
||||
} else if(urbitOb.isValidPatp(str) && !isInCodeBlock) {
|
||||
if (message.length > 0) {
|
||||
if (currTextLine.length > 0 || currTextBlock.length > 0) {
|
||||
// If we're in the middle of a message, add it to the stack and reset
|
||||
messages.push({ text: message.join(' ') });
|
||||
message = [];
|
||||
currTextLine.push('');
|
||||
messages.push({ text: currTextBlock.join('\n') + currTextLine.join(' ') });
|
||||
currTextBlock = last ? [''] : [];
|
||||
currTextLine = [];
|
||||
}
|
||||
messages.push({ mention: str });
|
||||
message = [];
|
||||
currTextLine = [];
|
||||
|
||||
} else {
|
||||
message.push(str);
|
||||
currTextLine.push(str);
|
||||
}
|
||||
});
|
||||
}
|
||||
currTextBlock.push(currTextLine.join(' '))
|
||||
});
|
||||
|
||||
if (message.length) {
|
||||
if (currTextBlock.length) {
|
||||
// Add any remaining message
|
||||
messages.push({ text: message.join(' ') });
|
||||
messages.push({ text: currTextBlock.join('\n') });
|
||||
}
|
||||
return messages;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user