Skip to content

Commit

Permalink
feat(mount): support mounting of documents (#343)
Browse files Browse the repository at this point in the history
fixes #342
  • Loading branch information
tripodsan authored Jul 21, 2020
1 parent 01fb5c5 commit 0362da4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/MountConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function stripQuery(m, ...specialparams) {

const onedriveDecorator = {
test(m) {
return /https:\/\/.*\.sharepoint\.com/.test(m.url) || m.url.startsWith('https://1drv.ms/');
return /https:\/\/.*\.sharepoint\.com/.test(m.url) || m.url.startsWith('https://1drv.ms/') || m.url.startsWith('onedrive:');
},
decorate(m) {
return {
Expand All @@ -56,13 +56,15 @@ const onedriveDecorator = {

const googleDecorator = {
test(m) {
return !m.id && m.url.startsWith('https://drive.google.com/');
return m.url.startsWith('https://drive.google.com/') || m.url.startsWith('gdrive:');
},
decorate(m) {
return {
...stripQuery(m, 'fallbackPath'),
type: 'google',
id: m.url.split('/').pop(),
id: m.url.startsWith('gdrive:')
? m.url.split(':').pop()
: m.url.split('/').pop(),
};
},
};
Expand Down Expand Up @@ -91,12 +93,13 @@ class MountConfig extends SchemaDerivedConfig {
*/
match(resourcePath) {
const fullPath = resourcePath.endsWith('/') ? resourcePath : `${resourcePath}/`;
const docPath = `${resourcePath}.md`;

const [mp] = this.mountpoints
.filter((m) => fullPath.startsWith(m.path)) // beginning must match
.filter((m) => (m.isDocument ? docPath === m.path : fullPath.startsWith(m.path)))
.map((m) => ({
...m,
relPath: fullPath.substring(m.path.length - 1, fullPath.length - 1),
relPath: m.isDocument ? '' : fullPath.substring(m.path.length - 1, fullPath.length - 1),
}));

return mp || null;
Expand Down
11 changes: 9 additions & 2 deletions src/MountPointHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ const MountPointHandler = (decorators) => ({

const obj = typeof config === 'string' ? {
url: config,
path: path.endsWith('/') ? path : `${path}/`,
} : {
...config,
path: path.endsWith('/') ? path : `${path}/`,
};
// append trailing slash for paths w/o extension
const idxLastSlash = path.lastIndexOf('/');
const idx = path.lastIndexOf('.');
if (idxLastSlash !== path.length - 1 && idx < idxLastSlash) {
obj.path = `${path}/`;
} else {
obj.path = path;
obj.isDocument = idxLastSlash !== path.length - 1;
}

const decorator = decorators.find((d) => d.test(obj));

Expand Down
33 changes: 31 additions & 2 deletions test/mountpoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ describe('Mount Point Config Loading', () => {
assert.equal(cfg.mountpoints.length, 1);
assert.equal(cfg.mountpoints[0].path, '/');
assert.equal(cfg.mountpoints[0].url, 'https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog?csf=1&e=8Znxth');

const m1 = cfg.match('/index.md');
assert.equal(m1.type, 'onedrive');
assert.equal(m1.url, 'https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog?csf=1&e=8Znxth');
assert.equal(m1.relPath, '/index.md');
});

it('Empty Mount Points gets properly evaluated', async () => {
Expand All @@ -163,10 +168,11 @@ describe('Mount Point Config Loading', () => {
.init();
assert.equal(cfg.match('/nomach'), null);

const m1 = cfg.match('/ms/en/posts/testdocument');
const m1 = cfg.match('/ms/en/posts/testdocument.md');
assert.equal(m1.type, 'onedrive');
assert.equal(m1.isDocument, undefined);
assert.equal(m1.url, 'https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog');
assert.equal(m1.relPath, '/en/posts/testdocument');
assert.equal(m1.relPath, '/en/posts/testdocument.md');

const m2 = cfg.match('/ms/docs/different');
assert.equal(m2.type, 'onedrive');
Expand All @@ -177,6 +183,7 @@ describe('Mount Point Config Loading', () => {
const m3 = cfg.match('/gd/document42');
assert.equal(m3.type, 'google');
assert.equal(m3.url, 'https://drive.google.com/drive/u/0/folders/123456789');
assert.equal(m3.path, '/gd/');
assert.equal(m3.id, '123456789');
assert.equal(m3.fallbackPath, 'default.md');
assert.equal(m3.relPath, '/document42');
Expand All @@ -197,6 +204,28 @@ describe('Mount Point Config Loading', () => {
assert.equal(m6.url, 'https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog', 'is confused by slashes');
assert.equal(m6.relPath, '');

// onedrive document check with extension
const m7 = cfg.match('/onedrive-index');
assert.equal(m7.type, 'onedrive');
assert.equal(m7.path, '/onedrive-index.md');
assert.equal(m7.url, 'https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog/homepage.docx');
assert.equal(m7.isDocument, true);
assert.equal(m7.relPath, '');

// google drive document check
const m8 = cfg.match('/google-index');
assert.equal(m8.type, 'google');
assert.equal(m8.url, 'gdrive:complexitemid');
assert.equal(m8.id, 'complexitemid');
assert.equal(m8.isDocument, true);
assert.equal(m8.relPath, '');

// onedrive with onedrive uri
const m9 = cfg.match('/mswithid/foo');
assert.equal(m9.type, 'onedrive');
assert.equal(m9.url, 'onedrive:/drives/1234/items/5678');
assert.equal(m9.relPath, '/foo');

assert.equal(cfg.match('/mssoft'), null, 'requires trailing slash in matches');
});
});
7 changes: 5 additions & 2 deletions test/specs/mountconfigs/complex.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"url": "https://drive.google.com/drive/u/0/folders/123456789",
"fallbackPath": "default.md"
},
"/foo": "https://localhost:4502"
"/mswithid": "onedrive:/drives/1234/items/5678",
"/foo/": "https://localhost:4502",
"/google-index.md": "gdrive:complexitemid",
"/onedrive-index.md": "https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog/homepage.docx"
}
}
}
7 changes: 5 additions & 2 deletions test/specs/mountconfigs/complex.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mountpoints:
/ms/docs: https://adobe.sharepoint.com/sites/docs?fallbackPath=default.docx
/ms: https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog
/gd:
/gd:
url: https://drive.google.com/drive/u/0/folders/123456789
fallbackPath: default.md
/foo: https://localhost:4502
/mswithid: onedrive:/drives/1234/items/5678
/foo/: https://localhost:4502
/google-index.md: gdrive:complexitemid
/onedrive-index.md: https://adobe.sharepoint.com/sites/TheBlog/Shared%20Documents/theblog/homepage.docx

0 comments on commit 0362da4

Please sign in to comment.