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