diff options
author | Azul <azul@riseup.net> | 2020-06-16 15:38:22 +0300 |
---|---|---|
committer | Azul <azul@riseup.net> | 2020-08-11 16:26:25 +0300 |
commit | 59182ba93f808ff3ff64331632747196b6a9302c (patch) | |
tree | 64b9e67fa951a47e50d1bef6ddb0933e7eae7de8 /src/tests | |
parent | 9dfc8cd64d2f90bf70028088f1c4697e3f659547 (diff) |
rewrite links to local files
The markdown we create for links to local files looks like this:
```
[Check this out!](subdir/file.md?fileid=123)
```
Turn this into a link like this
```
<a href="?dir=/current&openfile=123&relPath=subdir/file.md">
Check this out!
</a>
```
The `relPath` part of the url is needed
to convert it back to the original markdown.
Includes a test.
Next steps:
* open links to local files in same tab.
* change dir param according to relative Path.
Signed-off-by: Azul <azul@riseup.net>
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/helpers/links.spec.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/tests/helpers/links.spec.js b/src/tests/helpers/links.spec.js new file mode 100644 index 000000000..9317e320c --- /dev/null +++ b/src/tests/helpers/links.spec.js @@ -0,0 +1,88 @@ +import { domHref, parseHref } from '../../helpers/links' + +global.OC = { + Util: { + History: { + parseUrlQuery: () => ({ + dir: '/Wiki', + }), + }, + }, +} + +describe('Preparing href attributes for the DOM', () => { + + test('leave empty hrefs alone', () => { + expect(domHref({attrs: {href: ''}})).toBe('') + }) + + test('leave undefined hrefs alone', () => { + expect(domHref({attrs: {}})).toBe(undefined) + }) + + test('full url', () => { + expect(domHref({attrs: {href: 'https://otherdomain.tld'}})) + .toBe('https://otherdomain.tld') + }) + + test('other protocol', () => { + expect(domHref({attrs: {href: 'mailTo:test@mail.example'}})) + .toBe('mailTo:test@mail.example') + }) + + test('relative link with fileid', () => { + expect(domHref({attrs: {href: 'otherfile?fileId=123'}})) + .toBe('?dir=/Wiki&openfile=123&relPath=otherfile') + }) + +}) + +describe('Extracting short urls from the DOM', () => { + + const domStub = (href) => ({ + getAttribute() { + return href + }, + }) + + test('leave empty hrefs alone', () => { + expect(parseHref(domStub(''))).toBe('') + }) + + test('leave undefined hrefs alone', () => { + expect(parseHref(domStub())).toBe(undefined) + }) + + test('relative link with fileid', () => { + expect(parseHref(domStub('?dir=/Wiki&openfile=123&relPath=otherfile'))) + .toBe('otherfile?fileId=123') + }) + +}) + +describe('Inserting hrefs into the dom and extracting them again', () => { + + function insertAndExtract(attrs) { + const node = {attrs} + const dom = { + getAttribute() { + return domHref(node) + }, + } + return parseHref(dom) + } + + test('leave empty hrefs alone', () => { + expect(insertAndExtract({href: ''})).toBe('') + }) + + test('leave undefined hrefs alone', () => { + expect(insertAndExtract({})).toBe(undefined) + }) + + test('default relative link format is unchanged', () => { + expect(insertAndExtract({href: 'otherfile?fileId=123'})) + .toBe('otherfile?fileId=123') + }) + +}) |