Skip to content

Commit

Permalink
markdown: fix links
Browse files Browse the repository at this point in the history
  • Loading branch information
Wattenberger committed Aug 22, 2022
1 parent b069116 commit de3c5f9
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions blocks/file-blocks/markdown-edit/copy-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const copy = ({
const tag = /<(?<tag>[^/\s>]*)/.exec(text)?.groups?.tag;
if (tag === "a") {
const linkRegexHtml =
/<a.*?href="(?<url>.*?)".*?>(?<text>.*?)[<\/a>]*/;
/<a.*?href="(?<url>.*?)".*?>(?<linkText>[^]*?)[<\/a>]*/gm;
let urlResult = linkRegexHtml.exec(text);
if (urlResult && urlResult.groups && urlResult.groups.url) {
if (!text.includes("</a>")) {
Expand All @@ -243,33 +243,42 @@ export const copy = ({

to = to + endTagIndex;
text = state.doc.sliceString(from, to);
const linkRegexHtml =
/<a.*?href="(?<url>.*?)".*?>(?<text>.*?)<\/a>/;
urlResult = linkRegexHtml.exec(text);
}
let linkText = urlResult.groups.text;
const linkRegexHtml =
/<a.*?href="(?<url>.*?)".*?>(?<linkText>[^]*?)<\/a>/gm;
const newUrlResult = linkRegexHtml.exec(text);
if (newUrlResult) urlResult = newUrlResult;
let linkText = urlResult.groups.linkText;
const url = urlResult.groups.url;
if (url) {
const absoluteUrl = parseUrl(url, context);
const newAltDecoration = linkAltDecoration(
text,
linkText,
absoluteUrl
);
const altIndexStart = from + text.indexOf(linkText);
if (linkText)
widgets.push(
newAltDecoration.range(
altIndexStart,
altIndexStart + linkText.length
)
const includesHtml = /<.*?>/.exec(linkText);
if (includesHtml) {
const newDecoration = htmlTagDecoration({ text, context });
widgets.push(newDecoration.range(from, from));
const newAltDecoration = htmlTagTextDecoration({ text });
widgets.push(newAltDecoration.range(from, to));
} else {
const absoluteUrl = parseUrl(url, context);
const newAltDecoration = linkAltDecoration(
text,
linkText,
absoluteUrl
);
const newDecoration = linkDecoration(
text,
linkText,
absoluteUrl
);
widgets.push(newDecoration.range(from, to));
const altIndexStart = from + text.indexOf(linkText);
if (linkText)
widgets.push(
newAltDecoration.range(
altIndexStart,
altIndexStart + linkText.length
)
);
const newDecoration = linkDecoration(
text,
linkText,
absoluteUrl
);
widgets.push(newDecoration.range(from, to));
}
}
} else if (text === "</a>") {
const newAltDecoration = linkDecoration(text, "", "");
Expand Down

0 comments on commit de3c5f9

Please sign in to comment.