prepackage markdown extension

This commit is contained in:
kieferrm 2016-05-21 00:39:12 -07:00
parent 3112f9d236
commit 0b856de5a7
10 changed files with 2358 additions and 7 deletions

View file

@ -11,6 +11,7 @@ const extensions = [
'vscode-colorize-tests',
'json',
'configuration-editing',
'markdown',
'typescript',
'php',
'javascript',

View file

@ -0,0 +1,26 @@
{
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": "//",
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
"blockComment": [
"/*",
"*/"
]
},
// symbols used as brackets
"brackets": [
[
"{",
"}"
],
[
"[",
"]"
],
[
"(",
")"
]
]
}

View file

@ -0,0 +1,163 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: "Segoe WPC", "Segoe UI", "SFUIText-Light", "HelveticaNeue-Light", sans-serif, "Droid Sans Fallback";
font-size: 14px;
padding-left: 12px;
line-height: 22px;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
color: #4080D0;
text-decoration: none;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
a:hover {
color: #4080D0;
text-decoration: underline;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left: 5px solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 14px;
line-height: 19px;
}
.mac code {
font-size: 12px;
line-height: 18px;
}
code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
/** Theming */
.vs {
color: rgb(30, 30, 30);
}
.vs-dark {
color: #DDD;
}
.hc-black {
color: white;
}
.vs code {
color: #A31515;
}
.vs-dark code {
color: #D7BA7D;
}
.vs code > div {
background-color: rgba(220, 220, 220, 0.4);
}
.vs-dark code > div {
background-color: rgba(10, 10, 10, 0.4);
}
.hc-black code > div {
background-color: rgb(0, 0, 0);
}
.hc-black h1 {
border-color: rgb(0, 0, 0);
}
.vs table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vs-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vs h1,
.vs hr,
.vs table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vs-dark h1,
.vs-dark hr,
.vs-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
.vs blockquote,
.vs-dark blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.hc-black blockquote {
background: transparent;
border-color: #fff;
}

View file

@ -0,0 +1,71 @@
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}

View file

@ -1,12 +1,77 @@
{
"name": "markdown",
"version": "0.1.0",
"publisher": "vscode",
"engines": { "vscode": "*" },
"contributes": {
"snippets": [{
"name": "vscode-markdown",
"displayName": "VS Code Markdown",
"description": "Markdown for VS Code",
"version": "0.2.0",
"publisher": "Microsoft",
"engines": {
"vscode": "^1.0.0"
},
"main": "./out/extension",
"categories": [
"Languages"
],
"activationEvents": [
"onCommand:extension.previewMarkdown",
"onCommand:extension.previewMarkdownSide"
],
"contributes": {
"languages": [
{
"id": "markdown",
"aliases": [
"Markdown",
"markdown"
],
"extensions": [
".md",
".mdown",
".markdown",
".markdn"
],
"configuration": "./markdown.configuration.json"
}
],
"grammars": [
{
"language": "markdown",
"scopeName": "text.html.markdown",
"path": "./syntaxes/markdown.tmLanguage"
}
],
"commands": [
{
"command": "extension.previewMarkdown",
"title": "%markdown.previewMarkdown.title%"
},
{
"command": "extension.previewMarkdownSide",
"title": "%markdown.previewMarkdownSide.title%"
}
],
"keybindings": [
{
"command": "extension.previewMarkdown",
"key": "shift+ctrl+v",
"mac": "shift+cmd+v"
},
{
"command": "extension.previewMarkdownSide",
"key": "ctrl+k v",
"mac": "cmd+k v"
}
],
"snippets": [{
"language": "markdown",
"path": "./snippets/markdown.json"
}]
}
},
"scripts": {
"vscode:prepublish": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:markdown ./tsconfig.json"
},
"dependencies": {
"highlight.js": "^9.3.0",
"markdown-it": "^6.0.1",
"markdown-it-named-headers": "0.0.4"
}
}

View file

@ -0,0 +1,4 @@
{
"markdown.previewMarkdown.title" : "Markdown: Toggle Preview",
"markdown.previewMarkdownSide.title" : "Markdown: Open Preview to the Side"
}

View file

@ -0,0 +1,174 @@
/*---------------------------------------------------------------------------------------------
* 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 * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { ExtensionContext, TextDocumentContentProvider, EventEmitter, Event, Uri, TextDocument, ViewColumn } from "vscode";
const hljs = require('highlight.js');
const mdnh = require('markdown-it-named-headers');
const md = require('markdown-it')({
html: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre class="hljs"><code><div>${hljs.highlight(lang, str, true).value}</div></code></pre>`;
} catch (error) { }
}
return `<pre class="hljs"><code><div>${md.utils.escapeHtml(str)}</div></code></pre>`;
}
}).use(mdnh, {});
export function activate(context: ExtensionContext) {
let provider = new MDDocumentContentProvider(context);
let registration = vscode.workspace.registerTextDocumentContentProvider('markdown', provider);
let d1 = vscode.commands.registerCommand('extension.previewMarkdown', () => openPreview());
let d2 = vscode.commands.registerCommand('extension.previewMarkdownSide', () => openPreview(true));
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.onDidChangeConfiguration(() => {
vscode.workspace.textDocuments.forEach((document) => {
if ('markdown' === document.uri.scheme) {
provider.update(document.uri);
}
});
});
}
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(fileName: string) {
return fileName && (endsWith(fileName,'.md')
|| endsWith(fileName, '.mdown')
|| endsWith(fileName, '.markdown')
|| endsWith(fileName, '.markdn'));
}
function openPreview(sideBySide?: boolean): void {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
vscode.commands.executeCommand('workbench.action.navigateBack');
return;
}
let markdownPreviewUri = Uri.parse(`markdown://${activeEditor.document.uri.path}`);
vscode.commands.executeCommand('vscode.previewHtml', markdownPreviewUri, getViewColumn(sideBySide));
}
function getViewColumn(sideBySide): ViewColumn {
const active = vscode.window.activeTextEditor;
if (!active) {
return ViewColumn.One;
}
if (!sideBySide) {
return active.viewColumn;
}
switch (active.viewColumn) {
case ViewColumn.One:
return ViewColumn.Two;
case ViewColumn.Two:
return ViewColumn.Three;
}
return active.viewColumn;
}
class MDDocumentContentProvider implements TextDocumentContentProvider {
private context;
private _onDidChange = new EventEmitter<Uri>();
constructor(context: ExtensionContext) {
this.context = context;
}
private getMediaPath(mediaFile) {
return this.context.asAbsolutePath(path.join('media', mediaFile));
}
private fixHref(resource: Uri, href: string) {
if (href) {
// Return early if href is already a URL
if (Uri.parse(href).scheme) {
return href;
}
// Otherwise convert to a file URI by joining the href with the resource location
return Uri.file(path.join(path.dirname(resource.fsPath), href)).toString();
}
return href;
}
private computeCustomStyleSheetIncludes(uri) : string[] {
const styles = vscode.workspace.getConfiguration('markdown')['styles'];
if (styles && Array.isArray(styles)) {
return styles.map((style) => {
return `<link rel="stylesheet" href="${this.fixHref(uri, style)}" type="text/css" media="screen">`;
});
}
return [];
}
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 class="vs-theme-aware">'
).join('\n');
const body = md.render(buffer.toString());
const tail = [
'</body>',
'</html>'
].join('\n');
approve(head + body + tail);
});
});
}
get onDidChange(): Event<Uri> {
return this._onDidChange.event;
}
public update(uri: Uri) {
this._onDidChange.fire(uri);
}
}

View file

@ -0,0 +1,8 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/// <reference path='../../../declares.d.ts'/>
/// <reference path='../../../lib.core.d.ts'/>
/// <reference path='../../../node.d.ts'/>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "out",
"noLib": true,
"sourceMap": true,
"rootDir": "."
},
"exclude": [
"node_modules"
]
}