Skip to content

Commit

Permalink
fix: close #3, but now we lost the sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Feb 28, 2019
1 parent a11baf9 commit 8704083
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-it-vuese",
"version": "0.3.1",
"version": "0.4.0",
"description": "📖 Vuese plugin for markdown-it markdown parser",
"main": "dist/index.cjs.js",
"files": [
Expand Down Expand Up @@ -50,9 +50,9 @@
"@vuese/parser": "^2.0.2"
},
"devDependencies": {
"@babel/core": "^7.3.3",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/preset-env": "^7.3.1",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"babel-core": "^7.0.0-bridge.0",
Expand All @@ -75,15 +75,15 @@
"markdown-it-custom-block": "^0.1.1",
"markdown-it-include": "^1.1.0",
"rimraf": "^2.6.3",
"rollup": "^1.2.3",
"rollup": "^1.3.1",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^9.2.1",
"rollup-plugin-eslint": "^5.0.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-uglify": "^6.0.2",
"vuepress": "^1.0.0-alpha.39"
"vuepress": "^1.0.0-alpha.40"
},
"peerDependencies": {
"markdown-it": "^8.4.2"
Expand Down
60 changes: 39 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,54 @@ module.exports = (md, options = {}) => {
renderOptions = {},
} = options

md.core.ruler.after('inline', ruleName, (state) => {
if (!vueseRe.test(state.src)) return

for (let i = state.tokens.length - 1; i >= 0; i--) {
const token = state.tokens[i]

if (token.type !== 'inline') continue
md.block.ruler.before('fence', ruleName, getPathToken)
md.renderer.rules[ruleName] = (tokens, idx, _, env) => {
const { rawPath } = tokens[idx]
const filePath = getFilePath(rawPath)
const rawMd = getMdFromSfc(filePath)

/* istanbul ignore next */
if (env.loader) {
// for @vuepress/markdown-loader
env.loader.addDependency(filePath)
}

const match = vueseRe.exec(token.content)
if (!match || match.length < 2) continue
// save attributes, like hoistedTags
const tmpMd = Object.assign({}, md)
const res = md.render(rawMd, env)

const rawPath = match[1].trim()
const mdContent = getMdFromSfc(rawPath)
const vueseTokens = md.parse(mdContent, {})
// restore attributes
Object.assign(md, tmpMd)

state.tokens = state.tokens
.slice(0, i - 1)
// replace tokens
.concat(vueseTokens)
.concat(state.tokens.slice(i + 2))
}
})
return res.html || res
}

function getMdFromSfc (rawPath) {
function getFilePath (rawPath) {
const tmpPath = rawPath
.replace(/^['|"]/, '')
.replace(/['|"]$/, '')
.replace(/^@/, root)
const filePath = path.resolve(root, tmpPath)

return path.resolve(root, tmpPath)
}

function getPathToken (state, startLine) {
const pos = state.bMarks[startLine] + state.tShift[startLine]
const max = state.eMarks[startLine]
const match = vueseRe.exec(state.src.slice(pos, max))

if (!match || match.length < 2) return false

const rawPath = match[1].trim()
const token = state.push(ruleName, 'div', 0)

token.rawPath = rawPath
state.line = startLine + 1

return true
}

function getMdFromSfc (filePath) {
if (!fs.existsSync(filePath)) return `Not found: ${filePath}`

try {
Expand Down

0 comments on commit 8704083

Please sign in to comment.