Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
计算机应用开发
前端
Vuepress
提交
292e4bc3
V
Vuepress
项目概览
计算机应用开发
/
前端
/
Vuepress
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
Vuepress
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
292e4bc3
编写于
5月 27, 2018
作者:
U
ULIVZ
提交者:
GitHub
5月 27, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: support generic markdown file path reference (#509)
上级
5fcac1b8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
132 addition
and
6 deletion
+132
-6
lib/markdown/link.js
lib/markdown/link.js
+30
-6
test/markdown/__snapshots__/link.spec.js.snap
test/markdown/__snapshots__/link.spec.js.snap
+25
-0
test/markdown/link.spec.js
test/markdown/link.spec.js
+77
-0
未找到文件。
lib/markdown/link.js
浏览文件 @
292e4bc3
...
...
@@ -2,6 +2,8 @@
// 1. adding target="_blank" to external links
// 2. converting internal links to <router-link>
const
indexRE
=
/
(
.*
)(
index|readme
)
.md
(
#
?
.*
)
$/i
module
.
exports
=
(
md
,
externalAttrs
)
=>
{
let
hasOpenRouterLink
=
false
let
hasOpenExternalLink
=
false
...
...
@@ -37,15 +39,28 @@ module.exports = (md, externalAttrs) => {
const
links
=
md
.
__data
.
links
||
(
md
.
__data
.
links
=
[])
links
.
push
(
to
)
to
=
to
.
replace
(
/
\.
md$/
,
'
.html
'
)
.
replace
(
/
\.
md
(
#.*
)
$/
,
'
.html$1
'
)
// normalize links to README/index
if
(
/^index|readme
\.
html/i
.
test
(
to
))
{
to
=
'
/
'
const
indexMatch
=
to
.
match
(
indexRE
)
if
(
indexMatch
)
{
const
[,
path
,
,
hash
]
=
indexMatch
to
=
path
+
hash
}
else
{
to
=
to
.
replace
(
/
\.
md$/
,
'
.html
'
)
.
replace
(
/
\.
md
(
#.*
)
$/
,
'
.html$1
'
)
}
// relative path usage.
if
(
!
to
.
startsWith
(
'
/
'
))
{
to
=
ensureBeginningDotSlash
(
to
)
}
// markdown-it encodes the uri
link
[
1
]
=
decodeURI
(
to
)
// export the router links for testing
const
routerLinks
=
md
.
__data
.
routerLinks
||
(
md
.
__data
.
routerLinks
=
[])
routerLinks
.
push
(
to
)
return
Object
.
assign
({},
token
,
{
tag
:
'
router-link
'
})
...
...
@@ -65,3 +80,12 @@ module.exports = (md, externalAttrs) => {
return
self
.
renderToken
(
tokens
,
idx
,
options
)
}
}
const
beginningSlashRE
=
/^
\.\/
/
function
ensureBeginningDotSlash
(
path
)
{
if
(
beginningSlashRE
.
test
(
path
))
{
return
path
}
return
'
./
'
+
path
}
test/markdown/__snapshots__/link.spec.js.snap
0 → 100644
浏览文件 @
292e4bc3
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`link should render external links correctly 1`] = `
<p>
<a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
<OutboundLink/>
</a>
</p>
`;
exports[`link should render external links correctly 2`] = `
<p>
<a href="http://vuejs.org/" target="_blank" rel="noopener noreferrer">vue
<OutboundLink/>
</a>
</p>
`;
exports[`link should render external links correctly 3`] = `
<p>
<a href="https://google.com" target="_blank" rel="noopener noreferrer">some <strong>link</strong> with <code>code</code>
<OutboundLink/>
</a>
</p>
`;
test/markdown/link.spec.js
0 → 100644
浏览文件 @
292e4bc3
import
{
Md
}
from
'
./util
'
import
link
from
'
@/markdown/link.js
'
import
{
dataReturnable
}
from
'
@/markdown/index.js
'
const
mdL
=
Md
().
use
(
link
,
{
target
:
'
_blank
'
,
rel
:
'
noopener noreferrer
'
})
dataReturnable
(
mdL
)
const
internalLinkAsserts
=
{
// START abosolute path usage
'
/
'
:
'
/
'
,
'
/foo/
'
:
'
/foo/
'
,
'
/foo/#hash
'
:
'
/foo/#hash
'
,
'
/foo/two.md
'
:
'
/foo/two.html
'
,
'
/foo/two.html
'
:
'
/foo/two.html
'
,
// END abosolute path usage
// START relative path usage
'
README.md
'
:
'
./
'
,
'
./README.md
'
:
'
./
'
,
'
index.md
'
:
'
./
'
,
'
./index.md
'
:
'
./
'
,
'
one.md
'
:
'
./one.html
'
,
'
./one.md
'
:
'
./one.html
'
,
'
foo/README.md
'
:
'
./foo/
'
,
'
./foo/README.md
'
:
'
./foo/
'
,
'
foo/README.md#hash
'
:
'
./foo/#hash
'
,
'
./foo/README.md#hash
'
:
'
./foo/#hash
'
,
'
../README.md
'
:
'
./../
'
,
'
../README.md#hash
'
:
'
./../#hash
'
,
'
../foo.md
'
:
'
./../foo.html
'
,
'
../foo.md#hash
'
:
'
./../foo.html#hash
'
,
'
../foo/one.md
'
:
'
./../foo/one.html
'
,
'
../foo/one.md#hash
'
:
'
./../foo/one.html#hash
'
// END relative path usage
}
const
externalLinks
=
[
'
[vue](https://vuejs.org/)
'
,
'
[vue](http://vuejs.org/)
'
,
'
[some **link** with `code`](https://google.com)
'
// #496
]
describe
(
'
link
'
,
()
=>
{
test
(
'
should convert internal links to router links correctly
'
,
()
=>
{
for
(
const
before
in
internalLinkAsserts
)
{
const
input
=
`[
${
before
}
](
${
before
}
)`
const
output
=
mdL
.
render
(
input
)
const
after
=
getCompiledLink
(
output
)
expect
(
after
).
toBe
(
internalLinkAsserts
[
before
])
}
})
test
(
'
should render external links correctly
'
,
()
=>
{
for
(
const
link
of
externalLinks
)
{
const
{
html
}
=
mdL
.
render
(
link
)
expect
(
html
).
toMatchSnapshot
()
}
})
})
function
getCompiledLink
(
output
)
{
const
{
data
:
{
routerLinks
}}
=
output
return
routerLinks
[
0
]
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录