-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Convert item URLs to format that opens web library
Fixes #172
- Loading branch information
Showing
6 changed files
with
116 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
createZoteroItemMock, | ||
zoteroMock, | ||
} from '../../../../test/utils/zotero-mock'; | ||
import { getItemURL } from '../get-item-url'; | ||
|
||
const itemMock = createZoteroItemMock(); | ||
|
||
describe('getItemURL', () => { | ||
it('returns url using https', () => { | ||
zoteroMock.URI.getItemURI.mockReturnValueOnce( | ||
'http://zotero.org/users/local/ZswAJ4Qe/items/WGAMSPG3', | ||
); | ||
|
||
expect(getItemURL(itemMock)).toMatch(/^https:\/\/zotero.org/); | ||
}); | ||
|
||
describe('when item is in a user library', () => { | ||
it('returns local url when user is not signed in', () => { | ||
zoteroMock.URI.getItemURI.mockReturnValueOnce( | ||
'http://zotero.org/users/local/ZswAJ4Qe/items/WGAMSPG3', | ||
); | ||
zoteroMock.Users.getCurrentUsername.mockReturnValueOnce(undefined); | ||
|
||
expect(getItemURL(itemMock)).toStrictEqual( | ||
'https://zotero.org/users/local/ZswAJ4Qe/items/WGAMSPG3', | ||
); | ||
}); | ||
|
||
it('returns web url with username when user is signed in', () => { | ||
zoteroMock.URI.getItemURI.mockReturnValueOnce( | ||
'http://zotero.org/users/8509743/items/DE9YUFJ9', | ||
); | ||
zoteroMock.Users.getCurrentUsername.mockReturnValueOnce( | ||
'SOME user-name', | ||
); | ||
|
||
expect(getItemURL(itemMock)).toStrictEqual( | ||
'https://zotero.org/some__user-name/items/DE9YUFJ9', | ||
); | ||
}); | ||
}); | ||
|
||
describe('when item is in a group library', () => { | ||
it('returns web url', () => { | ||
zoteroMock.URI.getItemURI.mockReturnValueOnce( | ||
'http://zotero.org/groups/4873137/items/8XLITSYN', | ||
); | ||
zoteroMock.Users.getCurrentUsername.mockReturnValueOnce( | ||
'SOME user-name', | ||
); | ||
|
||
expect(getItemURL(itemMock)).toStrictEqual( | ||
'https://zotero.org/groups/4873137/items/8XLITSYN', | ||
); | ||
}); | ||
}); | ||
|
||
describe('when item is in a feed', () => { | ||
it('returns local url', () => { | ||
zoteroMock.URI.getItemURI.mockReturnValueOnce( | ||
'http://zotero.org/users/local/eHZqskJE/feeds/4/items/3R3PSB8X', | ||
); | ||
zoteroMock.Users.getCurrentUsername.mockReturnValueOnce( | ||
'SOME user-name', | ||
); | ||
|
||
expect(getItemURL(itemMock)).toStrictEqual( | ||
'https://zotero.org/users/local/eHZqskJE/feeds/4/items/3R3PSB8X', | ||
); | ||
}); | ||
}); | ||
}); |
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,30 @@ | ||
/** | ||
* Return URL to item in web library, if user is signed in. | ||
* If user is not signed in, the URLs will lead to a 404. | ||
* | ||
* @param item Zotero item to return URL for | ||
*/ | ||
export function getItemURL(item: Zotero.Item): string { | ||
const zoteroURI = Zotero.URI.getItemURI(item).replace(/^http:/, 'https:'); | ||
const username = Zotero.Users.getCurrentUsername(); | ||
|
||
if (!username) return zoteroURI; | ||
|
||
return zoteroURI.replace(/users\/(?!local\/)\w+/, slugify(username)); | ||
} | ||
|
||
/** | ||
* Generate url friendly slug from name | ||
* | ||
* From https://github.com/zotero/dataserver/blob/97267cbee6441350b66baff4c34e1f8f9833118a/model/Utilities.inc.php#L88-L100 | ||
* As suggested in https://forums.zotero.org/discussion/comment/422291/#Comment_422291 | ||
* | ||
* @param input name to generate slug from | ||
*/ | ||
function slugify(input: string) { | ||
return input | ||
.trim() | ||
.toLowerCase() | ||
.replace('/[^a-z0-9 ._-]/g', '') | ||
.replace(/ /g, '_'); | ||
} |
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