mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 20:06:26 -08:00
there could be so many more old link things that could be worked upon but i feel like i need a break from this thing 😭
This commit is contained in:
1 parent
6a68f4f468
commit
8d578b2e54
2 files changed
+11
-7
No files matched your search
@@ -198,11 +198,10 @@ const elementToPlainText = (node: CustomElement, children: string): string => {
|
||||
};
|
||||
|
||||
const SPOILERINPUTREGEX = /\|\|.+?\|\|/g;
|
||||
//very loose link check with the empty text at the end to make sure it doesnt overextend
|
||||
export const LINKINPUTREGEX = /\(?(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)\)?/g;
|
||||
const SPOILEREDLINKINPUTREGEX = /<(https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)>/g;
|
||||
const MASKEDSPOILEREDLINKINPUTREGEX =
|
||||
/<\[.+\]\((https?:\/\/[A-Za-z0-9-._~:/?#[\]()@!$&'*+,;%=]+)\)>/g;
|
||||
const LINK_URL = `(https?:\\/\\/.[A-Za-z0-9-._~:/?#[\\]()@!$&'*+,;%=]+)`;
|
||||
export const LINKINPUTREGEX = new RegExp(`\\(?(${LINK_URL})\\)?`, 'g');
|
||||
const SPOILEREDLINKINPUTREGEX = new RegExp(`<(${LINK_URL})>`, 'g');
|
||||
const MASKEDSPOILEREDLINKINPUTREGEX = new RegExp(`\\[.+\\]\\(${LINK_URL}\\)`, 'g');
|
||||
|
||||
/**
|
||||
* convert slate internal representation to a plain text string that can be sent to the server
|
||||
@@ -319,9 +318,15 @@ export const getMentions = (mx: MatrixClient, roomId: string, editor: Editor): M
|
||||
|
||||
export const getLinks = (serialized: Descendant | Descendant[]): string[] | undefined => {
|
||||
let finalList: string[] = [];
|
||||
let isInsideCodeBlock = false;
|
||||
const parseLinks = (node: Descendant): void => {
|
||||
if (Text.isText(node)) {
|
||||
let { text } = node;
|
||||
if (text.startsWith('```')) {
|
||||
isInsideCodeBlock = !isInsideCodeBlock;
|
||||
return;
|
||||
}
|
||||
if (isInsideCodeBlock) return;
|
||||
// get a list of all the urls and of the ones that are spoilered,
|
||||
// truncate the spoilered ones of their <> and then remove the items that are present in both lists
|
||||
const urlsMatch = text.match(LINKINPUTREGEX);
|
||||
@@ -359,7 +364,6 @@ export const getLinks = (serialized: Descendant | Descendant[]): string[] | unde
|
||||
finalList = finalList.concat(urls ?? []);
|
||||
return;
|
||||
}
|
||||
if (node.type === BlockType.CodeBlock || node.type === BlockType.CodeLine) return;
|
||||
node?.children?.forEach(parseLinks);
|
||||
};
|
||||
if (Array.isArray(serialized)) serialized.map((n) => parseLinks(n));
|
||||
|
||||
@@ -113,7 +113,7 @@ const LINK_REG_1 = new RegExp(`(<)?${LINK_ALT}${LINK_URL}(>)?`);
|
||||
export const LinkRule: InlineMDRule = {
|
||||
match: (text) => text.match(LINK_REG_1),
|
||||
html: (parse, match) => {
|
||||
let [, , g1, g2] = match;
|
||||
const [, , g1, g2] = match;
|
||||
if (!g1 || !g2) return '';
|
||||
if (g2.startsWith('<') && g2.endsWith('>'))
|
||||
return `<a data-md href="${g2.substring(4, g2.lastIndexOf('>'))}">${parse(g1)}</a>`;
|
||||
|
||||
Reference in new issue
Block a user