-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce MarkdownRenderer to avoid code duplication and to have the …
…one place for keeping the renderer options, #11877
- Loading branch information
Showing
5 changed files
with
69 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/vs/editor/contrib/markdown/browser/markdownRenderer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import { TPromise } from 'vs/base/common/winjs.base'; | ||
import { IMarkdownString } from 'vs/base/common/htmlContent'; | ||
import { renderMarkdown, RenderOptions } from 'vs/base/browser/htmlContentRenderer'; | ||
import { IOpenerService, NullOpenerService } from 'vs/platform/opener/common/opener'; | ||
import { IModeService } from 'vs/editor/common/services/modeService'; | ||
import URI from 'vs/base/common/uri'; | ||
import { onUnexpectedError } from 'vs/base/common/errors'; | ||
import { tokenizeToString } from 'vs/editor/common/modes/textToHtmlTokenizer'; | ||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; | ||
import { optional } from 'vs/platform/instantiation/common/instantiation'; | ||
|
||
export class MarkdownRenderer { | ||
|
||
private readonly _options: RenderOptions; | ||
|
||
constructor( | ||
editor: ICodeEditor, | ||
@IModeService private readonly _modeService: IModeService, | ||
@optional(IOpenerService) private readonly _openerService: IOpenerService = NullOpenerService, | ||
) { | ||
this._options = { | ||
actionCallback: (content) => { | ||
this._openerService.open(URI.parse(content)).then(void 0, onUnexpectedError); | ||
}, | ||
codeBlockRenderer: (languageAlias, value): string | TPromise<string> => { | ||
// In markdown, | ||
// it is possible that we stumble upon language aliases (e.g.js instead of javascript) | ||
// it is possible no alias is given in which case we fall back to the current editor lang | ||
const modeId = languageAlias | ||
? this._modeService.getModeIdForLanguageName(languageAlias) | ||
: editor.getModel().getLanguageIdentifier().language; | ||
|
||
return this._modeService.getOrCreateMode(modeId).then(_ => { | ||
return tokenizeToString(value, modeId); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
render(markdown: IMarkdownString, options?: RenderOptions): HTMLElement { | ||
if (options) { | ||
return renderMarkdown(markdown, { ...options, ...this._options }); | ||
} else { | ||
return renderMarkdown(markdown, this._options); | ||
} | ||
} | ||
} |