normallizeLink.js 966 字节
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const fs = require('fs')
const folderNames = []
fs.readdirSync('docs').forEach(item => {
  fs.lstatSync(`docs/${item}`).isDirectory() && folderNames.push(item)
})

function isExternal(path) {
  return /^[a-z]+:/i.test(path)
}

function normalizeLink(url) {
  if (!url.startsWith('/') && folderNames.some(item => url.split('/')[0] === item)) {
    return '/' + url
  }
  return url
}

module.exports = function (md) {
D
DCloud_LXH 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
	md.normalizeLink = (function (oldNormalizeLink) {
		return function (url) {
			url = isExternal(url)
				? url
				: normalizeLink(url)
						.replace(/(\bREADME\.md\b)|(\bREADME(?!\.))/i, 'index.html') // README.md | README | readme.md | readme -> index.html
						.replace(/(\bindex\.md\b)|(index(?!\.))/, 'index.html') // /index -> /index.html
						.replace(/\.md\b/, '.html') // *.md -> *.html
						.replace(/\?id=/, '#') // ?id= -> #
						.replace(/\\/g, '/') // \ -> /
			return oldNormalizeLink.bind(this)(url)
		}
	})(md.normalizeLink)
D
DCloud_LXH 已提交
32
}