Skip to content

Commit

Permalink
feat: add helper for disabled link, fixed #295 (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Oct 30, 2017
1 parent 1f04912 commit 4ad96f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
5 changes: 5 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ You will get `<a href="/demo/">link</a>`html. Do not worry, you can still set ti
[link](/demo2 ":target=_self")
```

## Disable link

```md
[link](/demo ":disabled")
```
50 changes: 34 additions & 16 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export class Compiler {
if (isFn(mdConf)) {
compile = mdConf(marked, renderer)
} else {
marked.setOptions(merge(mdConf, {
renderer: merge(renderer, mdConf.renderer)
}))
marked.setOptions(
merge(mdConf, {
renderer: merge(renderer, mdConf.renderer)
})
)
compile = marked
}

Expand Down Expand Up @@ -91,33 +93,49 @@ export class Compiler {
}
// highlight code
origin.code = renderer.code = function (code, lang = '') {
const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup)
const hl = Prism.highlight(
code,
Prism.languages[lang] || Prism.languages.markup
)

return `<pre v-pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
}
origin.link = renderer.link = function (href, title, text) {
let blank = ''
origin.link = renderer.link = function (href, title = '', text) {
let attrs = ''
const config = {}

if (!/:|(\/{2})/.test(href) &&
if (title) {
title = title
.replace(/:(\w+)=?(\w+)?/g, (m, key, value) => {
config[key] = value || true
return ''
})
.trim()
}

if (
!/:|(\/{2})/.test(href) &&
!_self.matchNotCompileLink(href) &&
!/(\s?:ignore)(\s\S+)?$/.test(title)) {
!config.ignore
) {
href = router.toURL(href, null, router.getCurrentPath())
} else {
blank = ` target="${linkTarget}"`
title = title && title.replace(/:ignore/g, '').trim()
attrs += ` target="${linkTarget}"`
}

if (config.target) {
attrs += ' target=' + config.target
}

let target = title && title.match(/:target=\w+/)
if (target) {
target = target[0]
title = title.replace(target, '')
blank = ' ' + target.slice(1)
if (config.disabled) {
attrs += ' disabled'
href = 'javascript:void(0)'
}

if (title) {
title = ` title="${title}"`
}
return `<a href="${href}"${title || ''}${blank}>${text}</a>`
return `<a href="${href}"${title || ''}${attrs}>${text}</a>`
}
origin.paragraph = renderer.paragraph = function (text) {
if (/^!&gt;/.test(text)) {
Expand Down
5 changes: 5 additions & 0 deletions src/themes/basic/_layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ img {
max-width: 100%;
}

a[disabled] {
cursor: not-allowed;
opacity: 0.6;
}

kbd {
border: solid 1px #ccc;
border-radius: 3px;
Expand Down

0 comments on commit 4ad96f3

Please sign in to comment.