Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
7465727a
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7465727a
编写于
6月 13, 2016
作者:
J
Johannes Rieken
提交者:
kieferrm
6月 14, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update preview on type, don't read document from disk
上级
f53bc234
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
46 addition
and
54 deletion
+46
-54
extensions/markdown/src/extension.ts
extensions/markdown/src/extension.ts
+46
-54
未找到文件。
extensions/markdown/src/extension.ts
浏览文件 @
7465727a
...
...
@@ -6,9 +6,8 @@
'
use strict
'
;
import
*
as
vscode
from
'
vscode
'
;
import
*
as
fs
from
'
fs
'
;
import
*
as
path
from
'
path
'
;
import
{
ExtensionContext
,
TextDocumentContentProvider
,
EventEmitter
,
Event
,
Uri
,
TextDocument
,
ViewColumn
}
from
"
vscode
"
;
import
{
ExtensionContext
,
TextDocumentContentProvider
,
EventEmitter
,
Event
,
Uri
,
ViewColumn
}
from
"
vscode
"
;
const
hljs
=
require
(
'
highlight.js
'
);
const
mdnh
=
require
(
'
markdown-it-named-headers
'
);
...
...
@@ -33,39 +32,37 @@ export function activate(context: ExtensionContext) {
context
.
subscriptions
.
push
(
d1
,
d2
,
registration
);
vscode
.
workspace
.
onDidSaveTextDocument
((
e
:
TextDocument
)
=>
{
if
(
isMarkdownFile
(
e
.
fileName
))
{
let
markdownPreviewUri
=
Uri
.
parse
(
`markdown://
${
e
.
uri
.
path
}
`
);
provider
.
update
(
markdownPreviewUri
);
vscode
.
workspace
.
onDidSaveTextDocument
(
document
=>
{
if
(
isMarkdownFile
(
document
))
{
const
uri
=
getMarkdownUri
(
document
);
provider
.
update
(
uri
);
}
});
vscode
.
workspace
.
onDidChangeTextDocument
(
event
=>
{
if
(
isMarkdownFile
(
event
.
document
))
{
const
uri
=
getMarkdownUri
(
event
.
document
);
provider
.
update
(
uri
);
}
});
vscode
.
workspace
.
onDidChangeConfiguration
(()
=>
{
vscode
.
workspace
.
textDocuments
.
forEach
((
document
)
=>
{
if
(
'
markdown
'
===
document
.
uri
.
schem
e
)
{
if
(
isMarkdownFil
e
)
{
provider
.
update
(
document
.
uri
);
}
});
});
}
// copy from src/vs/base/common/strings.ts
function
endsWith
(
haystack
:
string
,
needle
:
string
):
boolean
{
let
diff
=
haystack
.
length
-
needle
.
length
;
if
(
diff
>
0
)
{
return
haystack
.
lastIndexOf
(
needle
)
===
diff
;
}
else
if
(
diff
===
0
)
{
return
haystack
===
needle
;
}
else
{
return
false
;
}
function
isMarkdownFile
(
document
:
vscode
.
TextDocument
)
{
return
document
.
languageId
===
'
markdown
'
&&
document
.
uri
.
scheme
!==
'
markdown
'
;
// prevent processing of own documents
}
function
isMarkdownFile
(
fileName
:
string
)
{
return
fileName
&&
(
endsWith
(
fileName
,
'
.md
'
)
||
endsWith
(
fileName
,
'
.mdown
'
)
||
endsWith
(
fileName
,
'
.markdown
'
)
||
endsWith
(
fileName
,
'
.markdn
'
));
function
getMarkdownUri
(
document
:
vscode
.
TextDocument
)
{
return
document
.
uri
.
with
({
scheme
:
'
markdown
'
,
query
:
document
.
uri
.
toString
()
});
}
function
openPreview
(
sideBySide
?:
boolean
):
void
{
...
...
@@ -75,7 +72,7 @@ function openPreview(sideBySide?: boolean): void {
return
;
}
let
markdownPreviewUri
=
Uri
.
parse
(
`markdown://
${
activeEditor
.
document
.
uri
.
path
}
`
);
let
markdownPreviewUri
=
getMarkdownUri
(
activeEditor
.
document
);
vscode
.
commands
.
executeCommand
(
'
vscode.previewHtml
'
,
markdownPreviewUri
,
getViewColumn
(
sideBySide
));
}
...
...
@@ -101,15 +98,15 @@ function getViewColumn(sideBySide): ViewColumn {
class
MDDocumentContentProvider
implements
TextDocumentContentProvider
{
private
c
ontext
;
private
_context
:
ExtensionC
ontext
;
private
_onDidChange
=
new
EventEmitter
<
Uri
>
();
constructor
(
context
:
ExtensionContext
)
{
this
.
context
=
context
;
this
.
_
context
=
context
;
}
private
getMediaPath
(
mediaFile
)
{
return
this
.
context
.
asAbsolutePath
(
path
.
join
(
'
media
'
,
mediaFile
));
return
this
.
_
context
.
asAbsolutePath
(
path
.
join
(
'
media
'
,
mediaFile
));
}
private
fixHref
(
resource
:
Uri
,
href
:
string
)
{
...
...
@@ -124,7 +121,7 @@ class MDDocumentContentProvider implements TextDocumentContentProvider {
return
href
;
}
private
computeCustomStyleSheetIncludes
(
uri
)
:
string
[]
{
private
computeCustomStyleSheetIncludes
(
uri
:
Uri
)
:
string
[]
{
const
styles
=
vscode
.
workspace
.
getConfiguration
(
'
markdown
'
)[
'
styles
'
];
if
(
styles
&&
Array
.
isArray
(
styles
))
{
return
styles
.
map
((
style
)
=>
{
...
...
@@ -135,33 +132,28 @@ class MDDocumentContentProvider implements TextDocumentContentProvider {
}
public
provideTextDocumentContent
(
uri
:
Uri
):
Thenable
<
string
>
{
return
new
Promise
((
approve
,
reject
)
=>
{
fs
.
readFile
(
uri
.
fsPath
,
(
error
,
buffer
)
=>
{
if
(
error
)
{
return
reject
(
error
);
}
const
head
=
[].
concat
(
'
<!DOCTYPE html>
'
,
'
<html>
'
,
'
<head>
'
,
'
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
'
,
`<link rel="stylesheet" type="text/css" href="
${
this
.
getMediaPath
(
'
markdown.css
'
)}
" >`
,
`<link rel="stylesheet" type="text/css" href="
${
this
.
getMediaPath
(
'
tomorrow.css
'
)}
" >`
,
this
.
computeCustomStyleSheetIncludes
(
uri
),
'
</head>
'
,
'
<body>
'
).
join
(
'
\n
'
);
const
body
=
md
.
render
(
buffer
.
toString
());
const
tail
=
[
'
</body>
'
,
'
</html>
'
].
join
(
'
\n
'
);
approve
(
head
+
body
+
tail
);
});
return
vscode
.
workspace
.
openTextDocument
(
Uri
.
parse
(
uri
.
query
)).
then
(
document
=>
{
const
head
=
[].
concat
(
'
<!DOCTYPE html>
'
,
'
<html>
'
,
'
<head>
'
,
'
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
'
,
`<link rel="stylesheet" type="text/css" href="
${
this
.
getMediaPath
(
'
markdown.css
'
)}
" >`
,
`<link rel="stylesheet" type="text/css" href="
${
this
.
getMediaPath
(
'
tomorrow.css
'
)}
" >`
,
this
.
computeCustomStyleSheetIncludes
(
uri
),
'
</head>
'
,
'
<body>
'
).
join
(
'
\n
'
);
const
body
=
md
.
render
(
document
.
getText
());
const
tail
=
[
'
</body>
'
,
'
</html>
'
].
join
(
'
\n
'
);
return
head
+
body
+
tail
;
});
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录