Skip to content

Commit

Permalink
Update create link text handler
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Oct 8, 2024
1 parent a608679 commit df82fc4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
73 changes: 40 additions & 33 deletions src/mjs/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,66 +307,73 @@ export const createLinkText = (data = {}) => {
if (!isString(template)) {
throw new TypeError(`Expected String but got ${getType(template)}.`);
}
let content = isString(contentText) ? contentText.replace(/\s+/g, ' ') : '';
let linkTitle = title || '';
let linkUrl = url || '';
let content = isString(contentText)
? contentText.replace(/\s+/g, ' ').trim()
: '';
let linkTitle = isString(title) ? title.trim() : '';
let linkUrl = isString(url) ? url.trim() : '';
let attr = '';
switch (formatId) {
case ASCIIDOC:
content = escapeMatchingChars(content, /(\])/g) || '';
linkUrl = encodeUrlSpecialChar(linkUrl);
if (content) {
content = escapeMatchingChars(content, /(\])/g);
}
break;
case BBCODE_TEXT:
content =
stripMatchingChars(content, /\[(?:url(?:=.*)?|\/url)\]/gi) || '';
linkUrl = encodeUrlSpecialChar(linkUrl);
break;
case BBCODE_URL:
content = encodeUrlSpecialChar(content);
if (content) {
content = stripMatchingChars(content, /\[(?:url(?:=.*)?|\/url)\]/gi);
}
break;
case HTML_HYPER:
case HTML_PLAIN:
content = convertHtmlChar(content) || '';
linkTitle = convertHtmlChar(linkTitle) || '';
linkUrl = encodeUrlSpecialChar(linkUrl);
if (content) {
content = convertHtmlChar(content);
}
if (linkTitle) {
linkTitle = convertHtmlChar(linkTitle);
}
if (linkUrl) {
linkUrl = encodeUrlSpecialChar(linkUrl);
}
if (/#[^\f\n\r\t\x20]*:~:/.test(linkUrl)) {
attr += ' rel="noopener"';
}
break;
case LATEX:
content = convertLaTeXChar(content) || '';
if (content) {
content = convertLaTeXChar(content);
}
break;
case MARKDOWN:
content =
(content &&
escapeMatchingChars(convertHtmlChar(content), /([[\]])/g)) ||
'';
linkTitle =
(linkTitle &&
escapeMatchingChars(convertHtmlChar(linkTitle), /(")/g)) ||
'';
if (content) {
content = escapeMatchingChars(convertHtmlChar(content), /([[\]])/g);
}
if (linkTitle) {
linkTitle = escapeMatchingChars(convertHtmlChar(linkTitle), /(")/g);
}
break;
case MEDIAWIKI:
content = convertNumCharRef(content, /([[\]'~<>{}=*#;:\-|])/g) || '';
if (content) {
content = convertNumCharRef(content, /([[\]'~<>{}=*#;:\-|])/g);
}
break;
case ORG_MODE:
content = content.trim();
if (content) {
content = `[${content}]`;
}
break;
case REST:
content = escapeMatchingChars(content, /([`<>])/g) || '';
if (content) {
content = escapeMatchingChars(content, /([`<>])/g);
}
break;
case TEXTILE:
content =
(content && convertHtmlChar(convertNumCharRef(content, /([()])/g))) ||
'';
if (content) {
content = convertHtmlChar(convertNumCharRef(content, /([()])/g));
}
break;
default:
}
return template.replace(/%content%/g, content.trim())
.replace(/%url%/g, linkUrl.trim())
.replace(/%title%/g, linkTitle.trim())
.replace(/%attr%/g, attr);
return template.replace(/%content%/g, content).replace(/%url%/g, linkUrl)
.replace(/%title%/g, linkTitle).replace(/%attr%/g, attr);
};
36 changes: 22 additions & 14 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,16 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo bar');
const data = {
content: 'foo [bar] baz',
formatId: ASCIIDOC,
template: formatData[ASCIIDOC].template,
url: 'https://example.com/foo bar'
url
};
const res = await func(data);
assert.strictEqual(res,
'link:https://example.com/foo%20bar[foo [bar\\] baz]',
'result');
'link:https://example.com/foo%20bar[foo [bar\\] baz]', 'result');
});

it('should get string', async () => {
Expand All @@ -463,16 +463,16 @@ describe('format', () => {
url: 'https://example.com/foo'
};
const res = await func(data);
assert.strictEqual(res, 'link:https://example.com/foo[]',
'result');
assert.strictEqual(res, 'link:https://example.com/foo[]', 'result');
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo bar');
const data = {
content: 'foo [bar] baz',
formatId: BBCODE_TEXT,
template: formatData[BBCODE_TEXT].template,
url: 'https://example.com/foo bar'
url
};
const res = await func(data);
assert.strictEqual(res,
Expand All @@ -481,36 +481,39 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo bar');
const data = {
content: '',
formatId: BBCODE_TEXT,
template: formatData[BBCODE_TEXT].template,
url: 'https://example.com/foo bar'
url
};
const res = await func(data);
assert.strictEqual(res, '[url=https://example.com/foo%20bar][/url]',
'result');
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo bar');
const data = {
content: 'https://example.com/foo bar',
content: url,
formatId: BBCODE_URL,
template: formatData[BBCODE_URL].template,
url: 'https://example.com/foo bar'
url
};
const res = await func(data);
assert.strictEqual(res, '[url]https://example.com/foo%20bar[/url]',
'result');
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo?key=1&key2=2');
const data = {
content: 'foo "bar" baz',
formatId: HTML_HYPER,
template: formatData[HTML_HYPER].template,
title: 'foo&bar',
url: 'https://example.com/foo?key=1&key2=2'
url
};
const res = await func(data);
assert.strictEqual(
Expand All @@ -521,11 +524,12 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo?key=1&key2=2');
const data = {
content: 'foo "bar" baz',
formatId: HTML_HYPER,
template: formatData[HTML_HYPER].templateAlt,
url: 'https://example.com/foo?key=1&amp;key2=2'
url
};
const res = await func(data);
assert.strictEqual(
Expand Down Expand Up @@ -578,11 +582,13 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } =
new URL('https://example.com/foo#bar baz:~:text=baz%20qux');
const data = {
content: 'baz qux',
formatId: HTML_HYPER,
template: formatData[HTML_HYPER].templateAlt,
url: 'https://example.com/foo#bar baz:~:text=baz%20qux'
url
};
const res = await func(data);
assert.strictEqual(
Expand All @@ -593,12 +599,13 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo?key=1&key2=2');
const data = {
content: 'foo "bar" baz',
formatId: HTML_PLAIN,
template: formatData[HTML_PLAIN].template,
title: 'foo&bar',
url: 'https://example.com/foo?key=1&key2=2'
url
};
const res = await func(data);
assert.strictEqual(
Expand All @@ -609,11 +616,12 @@ describe('format', () => {
});

it('should get string', async () => {
const { href: url } = new URL('https://example.com/foo?key=1&amp;key2=2');
const data = {
content: 'foo "bar" baz',
formatId: HTML_PLAIN,
template: formatData[HTML_PLAIN].templateAlt,
url: 'https://example.com/foo?key=1&amp;key2=2'
url
};
const res = await func(data);
assert.strictEqual(
Expand Down

0 comments on commit df82fc4

Please sign in to comment.