Skip to content

Commit

Permalink
getPageByReference
Browse files Browse the repository at this point in the history
- PR fixes
  • Loading branch information
rodikh committed Oct 1, 2020
1 parent c5d9e26 commit 95c14e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/unibit.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,9 @@ module.exports = class Unibit {

getPageByFilePath(context, filePath) {
// remove extension
const urlPath = _.trim(filePath, '.md');
return _.find(context.site.pages, page => {
const pageUrl = _.trim(_.get(page, 'relPath'), '.md');
return urlPath === pageUrl;
const pageUrl = _.get(page, 'relPath');
return filePath === pageUrl;
});
}

Expand Down
29 changes: 12 additions & 17 deletions tests/unibit-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@ const Unibit = require('../src/unibit');
const unibit = new Unibit();

const aboutPage = {
"absPath": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content/about.md",
"relPath": "about.md",
"absDir": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content",
"relDir": "",
"url": "about/",
"basename": "about.md",
"filename": "about"
}
const feature1Page = {
"absPath": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content/features/feature1.md",
"relPath": "features/feature1.md",
"absDir": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content/features",
"relDir": "features",
"url": "features/feature1/",
"basename": "feature1.md",
"filename": "feature1"
};
const feature2Page = {
"absPath": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content/features/feature2.md",
"relPath": "features/feature2.md",
"absDir": "/Users/rodik/Work/Stackbit/project-repos/unibit/sample/unibit-universal/content/features",
"relDir": "features",
"url": "features/feature2/",
"basename": "feature2.md",
"filename": "feature2"
};

const linksSSGData = [
Expand Down Expand Up @@ -62,6 +47,16 @@ describe('getPage', ()=>{
it('gets a page by url', () => {
let page = unibit.getPage(pageContext, 'about');
expect(page).toBe(aboutPage);

page = unibit.getPage(pageContext, '/about');
expect(page).toBe(aboutPage);

page = unibit.getPage(pageContext, '/about/');
expect(page).toBe(aboutPage);

page = unibit.getPage(pageContext, 'about/');
expect(page).toBe(aboutPage);

});
it('should not work with file paths', () => {
let page = unibit.getPage(pageContext, 'about.md');
Expand Down Expand Up @@ -101,8 +96,8 @@ describe('getPageByFilePath', ()=>{
expect(data).toBe(feature1Page);
});

it('gets a page by url', () => {
it('should not work with urls', () => {
let data = unibit.getPageByFilePath(pageContext, 'features/feature1');
expect(data).toBe(feature1Page);
expect(data).toBeUndefined();
});
})

0 comments on commit 95c14e4

Please sign in to comment.