-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Axon 97] Fix image rendering in Jira Cloud tickets' description and …
…comments (#104) * Fix images in Jira Cloud tickets * fix linting * rewrote comment to make it clearer
- Loading branch information
Showing
6 changed files
with
145 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as html from './html'; | ||
|
||
describe('html util', () => { | ||
describe('fix relative URLs in html', () => { | ||
it('replaceRelativeURLsWithAbsolute correctly replaces the relative URL', () => { | ||
const baseUrl = 'https://www.domain.com/path1/path2/path3'; | ||
const htmlText = '<p><label>/imgs/test.png</label><img src="/imgs/test.png" /></p>'; | ||
|
||
const expectedHtml = | ||
'<p><label>/imgs/test.png</label><img src="https://www.domain.com/path1/path2/imgs/test.png" /></p>'; | ||
|
||
const fixedHtml = html.replaceRelativeURLsWithAbsolute(htmlText, baseUrl); | ||
expect(fixedHtml).toBe(expectedHtml); | ||
}); | ||
|
||
it('replaceRelativeURLsWithAbsolute correctly replaces all relative URLs', () => { | ||
const baseUrl = 'https://www.domain.com/path1/path2/path3'; | ||
const htmlText = | ||
'<p>' + | ||
'<label>/imgs/test1.png</label><img src="/imgs/test1.png" />' + | ||
'<label>/imgs/test2.png</label><img src="/imgs/test2.png" />' + | ||
"<label>/imgs/test3.png</label><img src='/imgs/test3.png' />" + | ||
"<label>/imgs/test4.png</label><img src='/imgs/test4.png' />" + | ||
'</p>'; | ||
|
||
const expectedHtml = | ||
'<p>' + | ||
'<label>/imgs/test1.png</label><img src="https://www.domain.com/path1/path2/imgs/test1.png" />' + | ||
'<label>/imgs/test2.png</label><img src="https://www.domain.com/path1/path2/imgs/test2.png" />' + | ||
"<label>/imgs/test3.png</label><img src='https://www.domain.com/path1/path2/imgs/test3.png' />" + | ||
"<label>/imgs/test4.png</label><img src='https://www.domain.com/path1/path2/imgs/test4.png' />" + | ||
'</p>'; | ||
|
||
const fixedHtml = html.replaceRelativeURLsWithAbsolute(htmlText, baseUrl); | ||
expect(fixedHtml).toBe(expectedHtml); | ||
}); | ||
|
||
it("replaceRelativeURLsWithAbsolute doesn't replaces absolute URLs", () => { | ||
const baseUrl = 'https://www.domain.com/path1/path2/path3'; | ||
const htmlText = '<p><label>/imgs/test.png</label><img src="https://www.domain.com/imgs/test.png" /></p>'; | ||
|
||
const fixedHtml = html.replaceRelativeURLsWithAbsolute(htmlText, baseUrl); | ||
expect(fixedHtml).toBe(htmlText); | ||
}); | ||
|
||
it("if there aren't relative URLs, nothing changes", () => { | ||
const baseUrl = 'https://www.domain.com/path1/path2/path3'; | ||
const htmlText = '<p><label>hello</label></p>'; | ||
|
||
const fixedHtml = html.replaceRelativeURLsWithAbsolute(htmlText, baseUrl); | ||
expect(fixedHtml).toBe(htmlText); | ||
}); | ||
|
||
it('last segment of the path is ignored unless it ends with a /', () => { | ||
const htmlText = '<img src="/imgs/test.png" />'; | ||
|
||
expect(html.replaceRelativeURLsWithAbsolute(htmlText, 'https://www.domain.com/path1/path2')).toBe( | ||
'<img src="https://www.domain.com/path1/imgs/test.png" />', | ||
); | ||
expect(html.replaceRelativeURLsWithAbsolute(htmlText, 'https://www.domain.com/path1/path2/')).toBe( | ||
'<img src="https://www.domain.com/path1/path2/imgs/test.png" />', | ||
); | ||
}); | ||
|
||
it('nullables are correctly handled', () => { | ||
expect(html.replaceRelativeURLsWithAbsolute('', 'https://www.domain.com/')).toBe(''); | ||
expect(html.replaceRelativeURLsWithAbsolute(null!, 'https://www.domain.com/')).toBe(null); | ||
expect(html.replaceRelativeURLsWithAbsolute(undefined!, 'https://www.domain.com/')).toBe(undefined); | ||
|
||
expect(html.replaceRelativeURLsWithAbsolute('<p></p>', '')).toBe('<p></p>'); | ||
expect(html.replaceRelativeURLsWithAbsolute('<p></p>', null!)).toBe('<p></p>'); | ||
expect(html.replaceRelativeURLsWithAbsolute('<p></p>', undefined!)).toBe('<p></p>'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const regex1 = / src="\/[^"]+/g; | ||
const regex2 = / src='\/[^']+/g; | ||
|
||
export function replaceRelativeURLsWithAbsolute(renderedHtml: string, baseApiUrl: string): string | undefined { | ||
if (!renderedHtml || !baseApiUrl) { | ||
return renderedHtml; | ||
} | ||
|
||
// The regex is searching for anything starting with ' src="/' which is 7 chars long, | ||
// and we need to get the relative URL without including its first /, so anything after those 7 characters. | ||
// Therefore, substring(7). | ||
return renderedHtml | ||
.replace(regex1, (x) => ` src=\"${new URL(x.substring(7), baseApiUrl).href}`) | ||
.replace(regex2, (x) => ` src=\'${new URL(x.substring(7), baseApiUrl).href}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters