Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
476d7fe0
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,发现更多精彩内容 >>
提交
476d7fe0
编写于
2月 05, 2019
作者:
M
Matt Bierner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor markdown contributions
Reducing code duplication and reducing state
上级
c39efd9a
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
94 addition
and
89 deletion
+94
-89
extensions/markdown-language-features/src/features/preview.ts
...nsions/markdown-language-features/src/features/preview.ts
+11
-11
extensions/markdown-language-features/src/features/previewContentProvider.ts
...-language-features/src/features/previewContentProvider.ts
+4
-4
extensions/markdown-language-features/src/features/previewManager.ts
...markdown-language-features/src/features/previewManager.ts
+2
-2
extensions/markdown-language-features/src/markdownEngine.ts
extensions/markdown-language-features/src/markdownEngine.ts
+3
-3
extensions/markdown-language-features/src/markdownExtensions.ts
...ions/markdown-language-features/src/markdownExtensions.ts
+71
-62
extensions/markdown-language-features/src/test/engine.ts
extensions/markdown-language-features/src/test/engine.ts
+3
-7
未找到文件。
extensions/markdown-language-features/src/features/preview.ts
浏览文件 @
476d7fe0
...
...
@@ -13,7 +13,7 @@ import { disposeAll } from '../util/dispose';
import
*
as
nls
from
'
vscode-nls
'
;
import
{
getVisibleLine
,
MarkdownFileTopmostLineMonitor
}
from
'
../util/topmostLineMonitor
'
;
import
{
MarkdownPreviewConfigurationManager
}
from
'
./previewConfig
'
;
import
{
MarkdownContributions
}
from
'
../markdownExtensions
'
;
import
{
MarkdownContribution
Provider
,
MarkdownContribution
s
}
from
'
../markdownExtensions
'
;
import
{
isMarkdownFile
}
from
'
../util/file
'
;
import
{
resolveLinkToMarkdownFile
}
from
'
../commands/openDocumentLink
'
;
const
localize
=
nls
.
loadMessageBundle
();
...
...
@@ -85,7 +85,7 @@ export class MarkdownPreview {
previewConfigurations
:
MarkdownPreviewConfigurationManager
,
logger
:
Logger
,
topmostLineMonitor
:
MarkdownFileTopmostLineMonitor
,
contribution
s
:
MarkdownContributions
,
contribution
Provider
:
MarkdownContributionProvider
,
):
Promise
<
MarkdownPreview
>
{
const
resource
=
vscode
.
Uri
.
parse
(
state
.
resource
);
const
locked
=
state
.
locked
;
...
...
@@ -99,9 +99,9 @@ export class MarkdownPreview {
previewConfigurations
,
logger
,
topmostLineMonitor
,
contribution
s
);
contribution
Provider
);
preview
.
editor
.
webview
.
options
=
MarkdownPreview
.
getWebviewOptions
(
resource
,
contributions
);
preview
.
editor
.
webview
.
options
=
MarkdownPreview
.
getWebviewOptions
(
resource
,
contribution
Provider
.
contribution
s
);
if
(
!
isNaN
(
line
))
{
preview
.
line
=
line
;
...
...
@@ -118,14 +118,14 @@ export class MarkdownPreview {
previewConfigurations
:
MarkdownPreviewConfigurationManager
,
logger
:
Logger
,
topmostLineMonitor
:
MarkdownFileTopmostLineMonitor
,
contribution
s
:
MarkdownContributions
contribution
Provider
:
MarkdownContributionProvider
):
MarkdownPreview
{
const
webview
=
vscode
.
window
.
createWebviewPanel
(
MarkdownPreview
.
viewType
,
MarkdownPreview
.
getPreviewTitle
(
resource
,
locked
),
previewColumn
,
{
enableFindWidget
:
true
,
...
MarkdownPreview
.
getWebviewOptions
(
resource
,
contributions
)
...
MarkdownPreview
.
getWebviewOptions
(
resource
,
contribution
Provider
.
contribution
s
)
});
return
new
MarkdownPreview
(
...
...
@@ -136,7 +136,7 @@ export class MarkdownPreview {
previewConfigurations
,
logger
,
topmostLineMonitor
,
contribution
s
);
contribution
Provider
);
}
private
constructor
(
...
...
@@ -147,7 +147,7 @@ export class MarkdownPreview {
private
readonly
_previewConfigurations
:
MarkdownPreviewConfigurationManager
,
private
readonly
_logger
:
Logger
,
topmostLineMonitor
:
MarkdownFileTopmostLineMonitor
,
private
readonly
_contribution
s
:
MarkdownContributions
,
private
readonly
_contribution
Provider
:
MarkdownContributionProvider
,
)
{
this
.
_resource
=
resource
;
this
.
_locked
=
locked
;
...
...
@@ -328,7 +328,7 @@ export class MarkdownPreview {
}
private
get
iconPath
()
{
const
root
=
path
.
join
(
this
.
_contribution
s
.
extensionPath
,
'
media
'
);
const
root
=
path
.
join
(
this
.
_contribution
Provider
.
extensionPath
,
'
media
'
);
return
{
light
:
vscode
.
Uri
.
file
(
path
.
join
(
root
,
'
Preview.svg
'
)),
dark
:
vscode
.
Uri
.
file
(
path
.
join
(
root
,
'
Preview_inverse.svg
'
))
...
...
@@ -392,7 +392,7 @@ export class MarkdownPreview {
if
(
this
.
_resource
===
resource
)
{
this
.
editor
.
title
=
MarkdownPreview
.
getPreviewTitle
(
this
.
_resource
,
this
.
_locked
);
this
.
editor
.
iconPath
=
this
.
iconPath
;
this
.
editor
.
webview
.
options
=
MarkdownPreview
.
getWebviewOptions
(
resource
,
this
.
_contributions
);
this
.
editor
.
webview
.
options
=
MarkdownPreview
.
getWebviewOptions
(
resource
,
this
.
_contribution
Provider
.
contribution
s
);
this
.
editor
.
webview
.
html
=
content
;
}
}
...
...
@@ -410,7 +410,7 @@ export class MarkdownPreview {
private
static
getLocalResourceRoots
(
resource
:
vscode
.
Uri
,
contributions
:
MarkdownContributions
):
vscode
.
Uri
[]
{
):
ReadonlyArray
<
vscode
.
Uri
>
{
const
baseRoots
=
contributions
.
previewResourceRoots
;
const
folder
=
vscode
.
workspace
.
getWorkspaceFolder
(
resource
);
...
...
extensions/markdown-language-features/src/features/previewContentProvider.ts
浏览文件 @
476d7fe0
...
...
@@ -13,7 +13,7 @@ const localize = nls.loadMessageBundle();
import
{
Logger
}
from
'
../logger
'
;
import
{
ContentSecurityPolicyArbiter
,
MarkdownPreviewSecurityLevel
}
from
'
../security
'
;
import
{
MarkdownPreviewConfigurationManager
,
MarkdownPreviewConfiguration
}
from
'
./previewConfig
'
;
import
{
MarkdownContribution
s
}
from
'
../markdownExtensions
'
;
import
{
MarkdownContribution
Provider
}
from
'
../markdownExtensions
'
;
/**
* Strings used inside the markdown preview.
...
...
@@ -40,7 +40,7 @@ export class MarkdownContentProvider {
private
readonly
engine
:
MarkdownEngine
,
private
readonly
context
:
vscode
.
ExtensionContext
,
private
readonly
cspArbiter
:
ContentSecurityPolicyArbiter
,
private
readonly
contribution
s
:
MarkdownContributions
,
private
readonly
contribution
Provider
:
MarkdownContributionProvider
,
private
readonly
logger
:
Logger
)
{
}
...
...
@@ -163,7 +163,7 @@ export class MarkdownContentProvider {
}
private
getStyles
(
resource
:
vscode
.
Uri
,
nonce
:
string
,
config
:
MarkdownPreviewConfiguration
,
state
?:
any
):
string
{
const
baseStyles
=
this
.
contributions
.
previewStyles
const
baseStyles
=
this
.
contribution
Provider
.
contribution
s
.
previewStyles
.
map
(
resource
=>
`<link rel="stylesheet" type="text/css" href="
${
resource
.
toString
()}
">`
)
.
join
(
'
\n
'
);
...
...
@@ -174,7 +174,7 @@ export class MarkdownContentProvider {
}
private
getScripts
(
nonce
:
string
):
string
{
return
this
.
contributions
.
previewScripts
return
this
.
contribution
Provider
.
contribution
s
.
previewScripts
.
map
(
resource
=>
`<script async src="
${
resource
.
toString
()}
" nonce="
${
nonce
}
" charset="UTF-8"></script>`
)
.
join
(
'
\n
'
);
}
...
...
extensions/markdown-language-features/src/features/previewManager.ts
浏览文件 @
476d7fe0
...
...
@@ -5,7 +5,7 @@
import
*
as
vscode
from
'
vscode
'
;
import
{
Logger
}
from
'
../logger
'
;
import
{
MarkdownContribution
s
}
from
'
../markdownExtensions
'
;
import
{
MarkdownContribution
Provider
}
from
'
../markdownExtensions
'
;
import
{
disposeAll
}
from
'
../util/dispose
'
;
import
{
MarkdownFileTopmostLineMonitor
}
from
'
../util/topmostLineMonitor
'
;
import
{
MarkdownPreview
,
PreviewSettings
}
from
'
./preview
'
;
...
...
@@ -25,7 +25,7 @@ export class MarkdownPreviewManager implements vscode.WebviewPanelSerializer {
public
constructor
(
private
readonly
_contentProvider
:
MarkdownContentProvider
,
private
readonly
_logger
:
Logger
,
private
readonly
_contributions
:
MarkdownContribution
s
private
readonly
_contributions
:
MarkdownContribution
Provider
)
{
this
.
_disposables
.
push
(
vscode
.
window
.
registerWebviewPanelSerializer
(
MarkdownPreview
.
viewType
,
this
));
}
...
...
extensions/markdown-language-features/src/markdownEngine.ts
浏览文件 @
476d7fe0
...
...
@@ -7,7 +7,7 @@ import * as crypto from 'crypto';
import
{
MarkdownIt
,
Token
}
from
'
markdown-it
'
;
import
*
as
path
from
'
path
'
;
import
*
as
vscode
from
'
vscode
'
;
import
{
MarkdownContribution
s
}
from
'
./markdownExtensions
'
;
import
{
MarkdownContribution
Provider
as
MarkdownContributionProvider
}
from
'
./markdownExtensions
'
;
import
{
Slugifier
}
from
'
./slugify
'
;
import
{
SkinnyTextDocument
}
from
'
./tableOfContentsProvider
'
;
import
{
getUriForLinkWithKnownExternalScheme
}
from
'
./util/links
'
;
...
...
@@ -57,7 +57,7 @@ export class MarkdownEngine {
private
_tokenCache
=
new
TokenCache
();
public
constructor
(
private
readonly
extensionPreviewResourceProvider
:
MarkdownContributions
,
private
readonly
contributionProvider
:
MarkdownContributionProvider
,
private
readonly
slugifier
:
Slugifier
,
)
{
}
...
...
@@ -66,7 +66,7 @@ export class MarkdownEngine {
this
.
md
=
import
(
'
markdown-it
'
).
then
(
async
markdownIt
=>
{
let
md
:
MarkdownIt
=
markdownIt
(
await
getMarkdownOptions
(()
=>
md
));
for
(
const
plugin
of
this
.
extensionPreviewResourceProvider
.
markdownItPlugins
)
{
for
(
const
plugin
of
this
.
contributionProvider
.
contributions
.
markdownItPlugins
)
{
try
{
md
=
(
await
plugin
)(
md
);
}
catch
{
...
...
extensions/markdown-language-features/src/markdownExtensions.ts
浏览文件 @
476d7fe0
...
...
@@ -26,96 +26,105 @@ const resolveExtensionResources = (extension: vscode.Extension<any>, resourcePat
};
export
interface
MarkdownContributions
{
readonly
extensionPath
:
string
;
readonly
previewScripts
:
vscode
.
Uri
[];
readonly
previewStyles
:
vscode
.
Uri
[];
readonly
markdownItPlugins
:
Thenable
<
(
md
:
any
)
=>
any
>
[];
readonly
previewResourceRoots
:
vscode
.
Uri
[];
readonly
previewScripts
:
ReadonlyArray
<
vscode
.
Uri
>
;
readonly
previewStyles
:
ReadonlyArray
<
vscode
.
Uri
>
;
readonly
previewResourceRoots
:
ReadonlyArray
<
vscode
.
Uri
>
;
readonly
markdownItPlugins
:
ReadonlyArray
<
Thenable
<
(
md
:
any
)
=>
any
>>
;
}
class
MarkdownExtensionContributions
implements
MarkdownContributions
{
private
readonly
_scripts
:
vscode
.
Uri
[]
=
[];
private
readonly
_styles
:
vscode
.
Uri
[]
=
[];
private
readonly
_previewResourceRoots
:
vscode
.
Uri
[]
=
[];
private
readonly
_plugins
:
Thenable
<
(
md
:
any
)
=>
any
>
[]
=
[];
private
_loaded
=
false
;
public
constructor
(
public
readonly
extensionPath
:
string
,
)
{
}
public
get
previewScripts
():
vscode
.
Uri
[]
{
this
.
ensureLoaded
();
return
this
.
_scripts
;
}
public
get
previewStyles
():
vscode
.
Uri
[]
{
this
.
ensureLoaded
();
return
this
.
_styles
;
}
public
get
previewResourceRoots
():
vscode
.
Uri
[]
{
this
.
ensureLoaded
();
return
this
.
_previewResourceRoots
;
}
public
get
markdownItPlugins
():
Thenable
<
(
md
:
any
)
=>
any
>
[]
{
this
.
ensureLoaded
();
return
this
.
_plugins
;
export
namespace
MarkdownContributions
{
export
const
Empty
:
MarkdownContributions
=
{
previewScripts
:
[],
previewStyles
:
[],
previewResourceRoots
:
[],
markdownItPlugins
:
[]
};
export
function
merge
(
a
:
MarkdownContributions
,
b
:
MarkdownContributions
):
MarkdownContributions
{
return
{
previewScripts
:
[...
a
.
previewScripts
,
...
b
.
previewScripts
],
previewStyles
:
[...
a
.
previewStyles
,
...
b
.
previewStyles
],
previewResourceRoots
:
[...
a
.
previewResourceRoots
,
...
b
.
previewResourceRoots
],
markdownItPlugins
:
[...
a
.
markdownItPlugins
,
...
b
.
markdownItPlugins
],
};
}
private
ensureLoaded
()
{
if
(
this
.
_loaded
)
{
return
;
export
function
fromExtension
(
extension
:
vscode
.
Extension
<
any
>
):
MarkdownContributions
{
const
contributes
=
extension
.
packageJSON
&&
extension
.
packageJSON
.
contributes
;
if
(
!
contributes
)
{
return
MarkdownContributions
.
Empty
;
}
this
.
_loaded
=
true
;
for
(
const
extension
of
vscode
.
extensions
.
all
)
{
const
contributes
=
extension
.
packageJSON
&&
extension
.
packageJSON
.
contributes
;
if
(
!
contributes
)
{
continue
;
}
this
.
tryLoadPreviewStyles
(
contributes
,
extension
);
this
.
tryLoadPreviewScripts
(
contributes
,
extension
);
this
.
tryLoadMarkdownItPlugins
(
contributes
,
extension
);
if
(
contributes
[
'
markdown.previewScripts
'
]
||
contributes
[
'
markdown.previewStyles
'
])
{
this
.
_previewResourceRoots
.
push
(
vscode
.
Uri
.
file
(
extension
.
extensionPath
));
}
const
styles
=
tryLoadPreviewStyles
(
contributes
,
extension
);
const
scripts
=
tryLoadPreviewScripts
(
contributes
,
extension
);
const
previewResourceRoots
:
vscode
.
Uri
[]
=
[];
if
(
styles
.
length
||
scripts
.
length
)
{
previewResourceRoots
.
push
(
vscode
.
Uri
.
file
(
extension
.
extensionPath
));
}
const
plugins
=
tryLoadMarkdownItPlugins
(
contributes
,
extension
);
return
{
previewScripts
:
scripts
,
previewStyles
:
styles
,
previewResourceRoots
,
markdownItPlugins
:
plugins
?
[
plugins
]
:
[]
};
}
private
tryLoadMarkdownItPlugins
(
function
tryLoadMarkdownItPlugins
(
contributes
:
any
,
extension
:
vscode
.
Extension
<
any
>
)
{
)
:
Thenable
<
(
md
:
any
)
=>
any
>
|
undefined
{
if
(
contributes
[
'
markdown.markdownItPlugins
'
])
{
this
.
_plugins
.
push
(
extension
.
activate
().
then
(()
=>
{
return
extension
.
activate
().
then
(()
=>
{
if
(
extension
.
exports
&&
extension
.
exports
.
extendMarkdownIt
)
{
return
(
md
:
any
)
=>
extension
.
exports
.
extendMarkdownIt
(
md
);
}
return
(
md
:
any
)
=>
md
;
})
)
;
});
}
return
undefined
;
}
private
tryLoadPreviewScripts
(
function
tryLoadPreviewScripts
(
contributes
:
any
,
extension
:
vscode
.
Extension
<
any
>
)
{
this
.
_scripts
.
push
(...
resolveExtensionResources
(
extension
,
contributes
[
'
markdown.previewScripts
'
])
);
return
resolveExtensionResources
(
extension
,
contributes
[
'
markdown.previewScripts
'
]
);
}
private
tryLoadPreviewStyles
(
function
tryLoadPreviewStyles
(
contributes
:
any
,
extension
:
vscode
.
Extension
<
any
>
)
{
this
.
_styles
.
push
(...
resolveExtensionResources
(
extension
,
contributes
[
'
markdown.previewStyles
'
]));
return
resolveExtensionResources
(
extension
,
contributes
[
'
markdown.previewStyles
'
]);
}
}
export
interface
MarkdownContributionProvider
{
readonly
extensionPath
:
string
;
readonly
contributions
:
MarkdownContributions
;
}
class
VSCodeExtensionMarkdownContributionProvider
implements
MarkdownContributionProvider
{
private
_contributions
?:
MarkdownContributions
;
public
constructor
(
public
readonly
extensionPath
:
string
,
)
{
}
public
get
contributions
():
MarkdownContributions
{
if
(
!
this
.
_contributions
)
{
this
.
_contributions
=
vscode
.
extensions
.
all
.
reduce
(
(
contributions
,
extension
)
=>
MarkdownContributions
.
merge
(
contributions
,
MarkdownContributions
.
fromExtension
(
extension
)),
MarkdownContributions
.
Empty
);
}
return
this
.
_contributions
;
}
}
export
function
getMarkdownExtensionContributions
(
context
:
vscode
.
ExtensionContext
):
MarkdownContribution
s
{
return
new
MarkdownExtensionContributions
(
context
.
extensionPath
);
export
function
getMarkdownExtensionContributions
(
context
:
vscode
.
ExtensionContext
):
MarkdownContribution
Provider
{
return
new
VSCodeExtensionMarkdownContributionProvider
(
context
.
extensionPath
);
}
\ No newline at end of file
extensions/markdown-language-features/src/test/engine.ts
浏览文件 @
476d7fe0
...
...
@@ -3,17 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
*
as
vscode
from
'
vscode
'
;
import
{
MarkdownEngine
}
from
'
../markdownEngine
'
;
import
{
MarkdownContributions
}
from
'
../markdownExtensions
'
;
import
{
MarkdownContribution
Provider
,
MarkdownContribution
s
}
from
'
../markdownExtensions
'
;
import
{
githubSlugifier
}
from
'
../slugify
'
;
const
emptyContributions
=
new
class
implements
MarkdownContribution
s
{
const
emptyContributions
=
new
class
implements
MarkdownContribution
Provider
{
readonly
extensionPath
=
''
;
readonly
previewScripts
:
vscode
.
Uri
[]
=
[];
readonly
previewStyles
:
vscode
.
Uri
[]
=
[];
readonly
previewResourceRoots
:
vscode
.
Uri
[]
=
[];
readonly
markdownItPlugins
:
Promise
<
(
md
:
any
)
=>
any
>
[]
=
[];
readonly
contributions
=
MarkdownContributions
.
Empty
;
};
export
function
createNewMarkdownEngine
():
MarkdownEngine
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录