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 03a830d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
75 changes: 40 additions & 35 deletions src/mjs/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,73 +300,78 @@ export const createTabsLinkText = (arr, opt = {}) => {
* @returns {string} - link text
*/
export const createLinkText = (data = {}) => {
const { content: contentText, formatId, template, title, url } = data;
let { content = '', formatId, template, title = '', url = '' } = data;
if (!isString(formatId)) {
throw new TypeError(`Expected String but got ${getType(formatId)}.`);
}
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 || '';
content = content.replace(/\s+/g, ' ').trim();
title = title.trim();
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 (/#[^\f\n\r\t\x20]*:~:/.test(linkUrl)) {
if (content) {
content = convertHtmlChar(content);
}
if (title) {
title = convertHtmlChar(title);
}
if (url) {
url = encodeUrlSpecialChar(url);
}
if (/#.*:~:/.test(url)) {
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 (title) {
title = escapeMatchingChars(convertHtmlChar(title), /(")/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, url)
.replace(/%title%/g, title).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 03a830d

Please sign in to comment.