forked from FoundKeyGang/FoundKey
[MFM] Better URL parsing
This commit is contained in:
parent
da10ba3fea
commit
e3b8495431
2 changed files with 33 additions and 6 deletions
|
@ -9,9 +9,11 @@ export type TextElementUrl = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function(text: string) {
|
export default function(text: string) {
|
||||||
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
|
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
|
||||||
if (!match) return null;
|
if (!match) return null;
|
||||||
const url = match[0];
|
let url = match[0];
|
||||||
|
if (url.endsWith('.')) url = url.substr(0, url.lastIndexOf('.'));
|
||||||
|
if (url.endsWith(',')) url = url.substr(0, url.lastIndexOf(','));
|
||||||
return {
|
return {
|
||||||
type: 'url',
|
type: 'url',
|
||||||
content: url,
|
content: url,
|
||||||
|
|
33
test/mfm.ts
33
test/mfm.ts
|
@ -160,12 +160,37 @@ describe('Text', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('url', () => {
|
it('url', () => {
|
||||||
const tokens = analyze('https://himasaku.net');
|
const tokens1 = analyze('https://example.com');
|
||||||
assert.deepEqual([{
|
assert.deepEqual([{
|
||||||
type: 'url',
|
type: 'url',
|
||||||
content: 'https://himasaku.net',
|
content: 'https://example.com',
|
||||||
url: 'https://himasaku.net'
|
url: 'https://example.com'
|
||||||
}], tokens);
|
}], tokens1);
|
||||||
|
|
||||||
|
const tokens2 = analyze('https://example.com.');
|
||||||
|
assert.deepEqual([{
|
||||||
|
type: 'url',
|
||||||
|
content: 'https://example.com',
|
||||||
|
url: 'https://example.com'
|
||||||
|
}, {
|
||||||
|
type: 'text', content: '.'
|
||||||
|
}], tokens2);
|
||||||
|
|
||||||
|
const tokens3 = analyze('https://example.com/foo?bar=a,b');
|
||||||
|
assert.deepEqual([{
|
||||||
|
type: 'url',
|
||||||
|
content: 'https://example.com/foo?bar=a,b',
|
||||||
|
url: 'https://example.com/foo?bar=a,b'
|
||||||
|
}], tokens3);
|
||||||
|
|
||||||
|
const tokens4 = analyze('https://example.com/foo, bar');
|
||||||
|
assert.deepEqual([{
|
||||||
|
type: 'url',
|
||||||
|
content: 'https://example.com/foo',
|
||||||
|
url: 'https://example.com/foo'
|
||||||
|
}, {
|
||||||
|
type: 'text', content: ', bar'
|
||||||
|
}], tokens4);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('link', () => {
|
it('link', () => {
|
||||||
|
|
Loading…
Reference in a new issue