Skip to content

Commit

Permalink
fix: rewrite getFilenameByPath
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 17, 2018
1 parent d83a424 commit c53bfe6
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 29 deletions.
7 changes: 5 additions & 2 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const IS_TEST = process.env.NODE_ENV === 'test'

module.exports = {
presets: [
['minimal', {
mode: 'loose'
}]
]
}],
IS_TEST && 'power-assert'
].filter(Boolean)
}
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:latest
- image: circleci/node:latest-browsers
branches:
ignore:
- gh-pages # list of branches to ignore
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"build:umd": "poi --config build/poi.lib.config.js --prod",
"build:es": "sfc normalize --config build/sfc.config.js",
"build": "npm run build:umd && npm run build:es",
"test": "npm run lint && echo 'no tests!'",
"test": "npm run lint && npm run test:unit",
"lint": "xo",
"website": "poi --serve --config build/poi.website.config.js",
"build:website": "poi --prod --config build/poi.website.config.js",
"prepublishOnly": "npm run build",
"commit": "git-cz"
"commit": "git-cz",
"test:unit": "poi puppet src/**/*.test.js --test --plugin @poi/puppet --framework mocha"
},
"dependencies": {
"isomorphic-unfetch": "^3.0.0",
Expand All @@ -29,9 +30,11 @@
"devDependencies": {
"@babel/plugin-syntax-object-rest-spread": "^7.0.0",
"@leptosia/docute-google-analytics": "^1.0.0",
"@poi/plugin-puppet": "^0.1.3",
"@semantic-release/changelog": "^3.0.1",
"@semantic-release/git": "^7.0.5",
"babel-preset-minimal": "^0.1.1",
"babel-preset-power-assert": "^3.0.0",
"bili": "^3.4.2",
"commitizen": "^3.0.5",
"cz-conventional-changelog": "^2.1.0",
Expand All @@ -43,6 +46,7 @@
"poi": "^12.1.5",
"postcss-import": "^12.0.0",
"postcss-preset-env": "^6.0.3",
"power-assert": "^1.6.1",
"rollup-plugin-vue": "^4.3.2",
"semantic-release": "^15.12.0",
"sfc": "^0.2.13",
Expand All @@ -57,7 +61,8 @@
"module": "lib/index.js",
"files": [
"dist",
"lib"
"lib",
"!**/__test__/**"
],
"description": "Effortlessly documentation done right.",
"author": "egoist <[email protected]>",
Expand All @@ -73,7 +78,8 @@
"**/dist/**"
],
"envs": [
"browser"
"browser",
"mocha"
],
"globals": [
"__DOCUTE_VERSION__"
Expand Down
15 changes: 15 additions & 0 deletions src/utils/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert'
import {getFilenameByPath} from '..'

describe('index', () => {
it('getFilenameByPath', () => {
assert(getFilenameByPath(null, '/') === 'README.md')
assert(getFilenameByPath('/', '/') === '/README.md')
assert(getFilenameByPath('./', '/') === 'README.md')
assert(getFilenameByPath('./docs/', '/') === 'docs/README.md')
assert(getFilenameByPath('/docs/', '/') === '/docs/README.md')
assert(getFilenameByPath('/docs/', '/foo') === '/docs/foo.md')
assert(getFilenameByPath('/docs/', '/foo.md') === '/docs/foo.md')
assert(getFilenameByPath('/docs/', '/foo/') === '/docs/foo/README.md')
})
})
20 changes: 12 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ export const slugify = str => {
}

export const getFilenameByPath = (sourcePath, path) => {
sourcePath = sourcePath || './'
sourcePath = sourcePath
// Remove leading relative path indicator, i.e. `.` and `./`
.replace(/^\.\/?/, '')
// Ensure the source path does not end with `/`
// Since out `path` has leading `/`
.replace(/(.+)\/?$/, '$1')
sourcePath = sourcePath || '.'

// Remove trailing slash in `sourcePath`
// Since `path` always starts with slash
sourcePath = sourcePath.replace(/\/$/, '')

// Ensure path always starts with slash
path = path.replace(/^\/?/, '/')

// Add .md suffix
if (!/\.md$/.test(path)) {
path = /\/$/.test(path) ? `${path}README.md` : `${path}.md`
}

return sourcePath + path
const result = sourcePath + path

return result.replace(/^\.\//, '')
}
Loading

0 comments on commit c53bfe6

Please sign in to comment.