Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for markdown links. #835

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 45 additions & 7 deletions app/client/lib/textUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,56 @@ For 'Link (in http://www.uk?)'
'url-regex' [ 'http://www.uk?)' ]
*/

// Match http or https then domain name (with optional port) then any text that ends with letter or number.
export const urlRegex = /(https?:\/\/[A-Za-z\d][A-Za-z\d-.]*(?!\.)(?::\d+)?(?:\/[^\s]*)?[\w\d/])/;
// Match http or https then domain name and capture markdown link text and URL separately
const urlRegex = /(?:\[(.*?)\]\()?https?:\/\/[A-Za-z\d][A-Za-z\d-.]*\.[A-Za-z]{2,}(?::\d+)?(?:\/[^\s)]*)?(?:\))?/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would match strings like [Google](https://www.google.com or https://www.google.com).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right @georgegevoian - the regex needs fix;
I will take a look how can I add tests.

Btw one of the CI tests of nbrowser is failing and I can't understand why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DetailView does not opens cell for editing when clicked on link looks to be failing because a localhost URL isn't being treated as a link (most likely a regression in the regex).

The other failures look unrelated - we'll be landing a fix on main soon.

Thanks.


/**
* Detects URLs in a text and returns list of tokens { value, isLink }
* Detects URLs in a text and returns list of tokens { value, link, isLink }
*/
export function findLinks(text: string): Array<{value: string, isLink: boolean}> {
export function findLinks(text: string): Array<{value: string, link: string, isLink: boolean}> {
if (!text) {
return [{ value: text, isLink: false }];
return [{ value: text, link: text, isLink: false }];
}
// urls will be at odd-number indices
return text.split(urlRegex).map((value, i) => ({ value, isLink : (i % 2) === 1}));

const tokens = [];
let lastIndex = 0;
text.replace(urlRegex, (match: string, markdownText: string, offset: number) => {
// Add text before the URL
if (offset > lastIndex) {
const currentValue = text.substring(lastIndex, offset);
tokens.push({ value: currentValue, link: currentValue, isLink: false });
}

// Extracting the actual URL and link text
let actualUrl, displayText;
if (markdownText) {
const markdownMatch = match.match(/\[(.*?)\]\((.*?)\)/);
if (markdownMatch && markdownMatch.length === 3) {
displayText = markdownMatch[1];
actualUrl = markdownMatch[2];
}
} else {
displayText = actualUrl = match;
}

// Add the URL
tokens.push({
value: displayText,
link: actualUrl,
isLink: true
});

lastIndex = offset + match.length;
return match;
});

// Add any remaining text after the last URL
if (lastIndex < text.length) {
const currentValue = text.substring(lastIndex);
tokens.push({ value: currentValue, link: currentValue, isLink: false });
}

return tokens;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/client/ui2018/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export async function onClickHyperLink(ev: MouseEvent, url: CellValue) {
export function makeLinks(text: string) {
try {
const domElements: DomArg[] = [];
for (const {value, isLink} of findLinks(text)) {
for (const {value, link, isLink} of findLinks(text)) {
if (isLink) {
// Wrap link with a span to provide hover on and to override wrapping.
domElements.push(cssMaybeWrap(
gristLink(value,
gristLink(link,
cssIconBackground(
icon("FieldLink", testId('tb-link-icon')),
dom.cls(cssHoverInText.className),
Expand Down