Skip to content

Commit

Permalink
feat(html): Pass sourceFile to data.url
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Nov 26, 2020
1 parent 1ea0705 commit da80d9f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
10 changes: 8 additions & 2 deletions modules/html/lib/__tests__/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('norska-html > data', () => {
it.each([['about/index.html', '/about/']])(
'%s => %s',
async (input, expected) => {
const actual = await current.url(input);
const actual = await current.url({ destinationFile: input });
expect(actual).toHaveProperty('here', expected);
}
);
Expand All @@ -58,10 +58,16 @@ describe('norska-html > data', () => {
['blog/index.html', '../'],
['blog/me/index.html', '../../'],
])('%s => %s', async (input, expected) => {
const actual = await current.url(input);
const actual = await current.url({ destinationFile: input });
expect(actual).toHaveProperty('pathToRoot', expected);
});
});
describe('sourceFile', () => {
it('nominal case', async () => {
const actual = await current.url({ sourceFile: 'examples/index.md' });
expect(actual).toHaveProperty('sourceFile', 'examples/index.md');
});
});
});
describe('runtime', () => {
it('get data from runtime in config', async () => {
Expand Down
18 changes: 11 additions & 7 deletions modules/html/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ module.exports = {
},
/**
* Data relative to the current URL, to help crafting links
* @param {string} destination Path to the file created
* @param {object} options Paths to the source and destination files
* @param {string} options.sourceFile Path to the source file
* @param {string} options.destinationFile Path to the destination file
* @returns {object} Url Data
**/
async url(destination = 'index.html') {
async url(options = {}) {
const { sourceFile, destinationFile = 'index.html' } = options;
const sourceData = await this.data();

const baseUrl = helper.isProduction()
? _.get(sourceData, 'site.url', '/')
: `http://127.0.0.1:${config.get('port')}`;

const fullPathDir = path.dirname(config.toPath(destination));
const fullPathDir = path.dirname(config.toPath(destinationFile));
const relativePathDir = path.relative(fullPathDir, config.to());
const pathToRoot = _.isEmpty(relativePathDir)
? './'
: `${relativePathDir}/`;

const here = destination.replace(/index\.html$/, '');
const here = destinationFile.replace(/index\.html$/, '');

return {
sourceFile,
base: baseUrl,
here: `/${here}`,
pathToRoot,
Expand All @@ -60,14 +64,14 @@ module.exports = {
},
/**
* Return the data to be passed to each compiled file
* @param {string} destination Path to the destination file created
* @param {object} options Path to the source and destination files
* @returns {object} Data object
**/
async all(destination) {
async all(options) {
await norskaData.warmCache();

const data = await this.data();
const url = await this.url(destination);
const url = await this.url(options);
const runtime = await this.runtime();
const tweaks = this.tweaks();

Expand Down
5 changes: 4 additions & 1 deletion modules/html/lib/pug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ module.exports = {

// Create a recursive compileData object that contains the pugMethods with
// the right context
const siteData = await data.all(options.to);
const siteData = await data.all({
sourceFile: options.from,
destinationFile: options.to,
});
const baseData = _.merge(
{},
siteData,
Expand Down

0 comments on commit da80d9f

Please sign in to comment.