Skip to content

Commit

Permalink
fix: path normalization, example correction, TS types, comments cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Feb 15, 2024
1 parent 1ea336d commit ad01194
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/fixtures
test/fixtures
lib
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ metalsmith
defaults: {
refs: {
docs_index: 'file:./docs/index.html',
globalLinks: metalsmith.metadata().globalLinks
globalLinks: 'metadata:globalLinks'
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Plugin } from 'metalsmith';

export default refs;
export type Options = {
key: string;
/**
* Limit ref substitution by glob pattern to a subset of files
*/
pattern?: string | string[];
};
/**
* A Metalsmith plugin to serve as a boilerplate for other core plugins
* A metalsmith plugin to refer to other files and global metadata from a file's refs property
*
* @param {Options} options
* @returns {import('metalsmith').Plugin}
*/
declare function refs(options: Options): Plugin;
declare function refs(options: Options): import('metalsmith').Plugin;
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative, dirname, isAbsolute } from 'path'
import { relative, dirname, isAbsolute, normalize } from 'path'
import get from 'dlv'

/**
Expand Down Expand Up @@ -34,7 +34,7 @@ function refs(options) {
function srcRelPath(subdir, path) {
if (isAbsolute(path)) path = path.slice(1)
const referringPath = metalsmith.path(metalsmith.source(), subdir)
return relative(metalsmith.source(), metalsmith.path(referringPath, path))
return relative(metalsmith.source(), metalsmith.path(referringPath, normalize(path)))
}

const meta = metalsmith.metadata()
Expand All @@ -53,13 +53,9 @@ function refs(options) {
})

if (withRefs.length) debug('Processing refs for %s file(s)', withRefs.length)
else debug.warn('No files with "refs" defined".')
else debug.warn('No files with "refs" defined.')

while (withRefs.length) {
/**
* @type {Object} file
* @property {Object<string, string>} file.refs
*/
const [path, file] = withRefs.shift()

Object.entries(file.refs).forEach(([name, ref]) => {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('@metalsmith/refs', function () {
equals(fixture('default/build'), fixture('default/expected'))
})

it('should resolve absolute refs', async function () {
it('should resolve absolute refs to metalsmith.source()', async function () {
await Metalsmith(fixture('absolute-refs')).env('DEBUG', process.env.DEBUG).use(plugin()).use(toJSON).build()
equals(fixture('absolute-refs/build'), fixture('absolute-refs/expected'))
})
Expand Down

0 comments on commit ad01194

Please sign in to comment.