Merge pull request #5131 from urbit/lf/fix-tokenize

tokenizeMessage: fix urls after inline urls
This commit is contained in:
matildepark 2021-07-30 10:39:00 -04:00 committed by GitHub
commit ab5765c13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import urbitOb from 'urbit-ob';
import { parsePermalink, permalinkToReference } from '~/logic/lib/permalinks';
const URL_REGEX = new RegExp(String(/^([^[\]]*?)(([\w\-\+]+:\/\/)[-a-zA-Z0-9:@;?&=\/%\+\.\*!'\(\),\$_\{\}\^~\[\]`#|]+[-a-zA-Z0-9:@;?&=\/%\+\*!'\(\)\$_\{\}\^~\[\]`#|])([\s\S]*)/.source));
const URL_REGEX = new RegExp(String(/^([\s\S]*?)(([\w\-\+]+:\/\/)[-a-zA-Z0-9:@;?&=\/%\+\.\*!'\(\),\$_\{\}\^~\[\]`#|]+[-a-zA-Z0-9:@;?&=\/%\+\*!'\(\)\$_\{\}\^~\[\]`#|])([\s\S]*)/.source));
const PATP_REGEX = /^([\s\S]*?)(~[a-z_-]+)([\s\S]*)/;
@ -18,7 +18,15 @@ export const isUrl = (str) => {
};
const raceRegexes = (str) => {
const link = str.match(URL_REGEX);
let link = str.match(URL_REGEX);
while(link?.[1]?.endsWith('(')) {
const resumePos = link[1].length + link[2].length;
const resume = str.slice(resumePos);
link = resume.match(URL_REGEX);
if(link) {
link[1] = str.slice(0, resumePos) + link[1];
}
}
const groupRef = str.match(GROUP_REGEX);
const mention = str.match(PATP_REGEX);
let pfix = str;

View File

@ -127,5 +127,15 @@ describe('tokenizeMessage', () => {
expect(text).toBe('oh no, ');
expect(reference.group).toBe('/ship/~sampel/group-123-abc');
});
it('should handle permalinks after inline urls', () => {
const example = 'test [test](https://tlon.io) web+urbitgraph://group/~middev/the-forge/graph/~littel-wolfur/writs-7082/170141184505164612398001831549075456000/2/170141184505164722986064231401764421632';
const [{ text }, { reference: { graph } }] = tokenizeMessage(example);
expect(text).toBe('test [test](https://tlon.io) ');
expect(graph.group).toBe('/ship/~middev/the-forge');
expect(graph.graph).toBe('/ship/~littel-wolfur/writs-7082');
expect(graph.index).toBe('/170141184505164612398001831549075456000/2/170141184505164722986064231401764421632');
});
});