Skip to content

Commit b42086b

Browse files
committed
Enable markdown language features for web/serverless
1 parent 53dc628 commit b42086b

7 files changed

Lines changed: 201 additions & 7 deletions

File tree

extensions/markdown-language-features/.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tsconfig.json
44
out/test/**
55
out/**
66
extension.webpack.config.js
7+
extension-browser.webpack.config.js
78
cgmanifest.json
89
yarn.lock
910
preview-src/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withDefaults = require('../shared.webpack.config');
11+
const path = require('path');
12+
13+
const clientConfig = withDefaults({
14+
context: __dirname,
15+
target: 'webworker',
16+
entry: {
17+
extension: './src/extension.ts'
18+
},
19+
resolve: {
20+
alias: {
21+
'vscode-extension-telemetry': path.resolve(__dirname, 'polyfills/vscode-extension-telemetry.js'),
22+
'vscode-nls': path.resolve(__dirname, 'polyfills/vscode-nls.js'),
23+
},
24+
}
25+
});
26+
27+
clientConfig.module.rules[0].use.shift(); // remove nls loader
28+
29+
module.exports = clientConfig;

extensions/markdown-language-features/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"vscode": "^1.20.0"
1313
},
1414
"main": "./out/extension",
15+
"browser": "./dist/extension.js",
1516
"categories": [
1617
"Programming Languages"
1718
],
@@ -327,7 +328,9 @@
327328
"watch": "npm run build-preview && gulp watch-extension:markdown-language-features",
328329
"vscode:prepublish": "npm run build-ext && npm run build-preview",
329330
"build-ext": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:markdown-language-features ./tsconfig.json",
330-
"build-preview": "webpack --mode production"
331+
"build-preview": "webpack --mode production",
332+
"compile-web": "npx webpack-cli --config extension-browser.webpack.config --mode none",
333+
"watch-web": "npx webpack-cli --config extension-browser.webpack.config --mode none --watch --info-verbosity verbose"
331334
},
332335
"dependencies": {
333336
"highlight.js": "9.15.10",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
9+
let TelemetryReporter = (function () {
10+
function TelemetryReporter(extensionId, extensionVersion, key) {
11+
}
12+
TelemetryReporter.prototype.updateUserOptIn = function (key) {
13+
};
14+
TelemetryReporter.prototype.createAppInsightsClient = function (key) {
15+
};
16+
TelemetryReporter.prototype.getCommonProperties = function () {
17+
};
18+
TelemetryReporter.prototype.sendTelemetryEvent = function (eventName, properties, measurements) {
19+
};
20+
TelemetryReporter.prototype.dispose = function () {
21+
};
22+
TelemetryReporter.TELEMETRY_CONFIG_ID = 'telemetry';
23+
TelemetryReporter.TELEMETRY_CONFIG_ENABLED_ID = 'enableTelemetry';
24+
return TelemetryReporter;
25+
}());
26+
exports.default = TelemetryReporter;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
9+
function format(message, args) {
10+
let result;
11+
// if (isPseudo) {
12+
// // FF3B and FF3D is the Unicode zenkaku representation for [ and ]
13+
// message = '\uFF3B' + message.replace(/[aouei]/g, '$&$&') + '\uFF3D';
14+
// }
15+
if (args.length === 0) {
16+
result = message;
17+
}
18+
else {
19+
result = message.replace(/\{(\d+)\}/g, function (match, rest) {
20+
let index = rest[0];
21+
let arg = args[index];
22+
let replacement = match;
23+
if (typeof arg === 'string') {
24+
replacement = arg;
25+
}
26+
else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {
27+
replacement = String(arg);
28+
}
29+
return replacement;
30+
});
31+
}
32+
return result;
33+
}
34+
35+
function localize(key, message) {
36+
let args = [];
37+
for (let _i = 2; _i < arguments.length; _i++) {
38+
args[_i - 2] = arguments[_i];
39+
}
40+
return format(message, args);
41+
}
42+
43+
function loadMessageBundle(file) {
44+
return localize;
45+
}
46+
47+
let MessageFormat;
48+
(function (MessageFormat) {
49+
MessageFormat["file"] = "file";
50+
MessageFormat["bundle"] = "bundle";
51+
MessageFormat["both"] = "both";
52+
})(MessageFormat = exports.MessageFormat || (exports.MessageFormat = {}));
53+
let BundleFormat;
54+
(function (BundleFormat) {
55+
// the nls.bundle format
56+
BundleFormat["standalone"] = "standalone";
57+
BundleFormat["languagePack"] = "languagePack";
58+
})(BundleFormat = exports.BundleFormat || (exports.BundleFormat = {}));
59+
60+
exports.loadMessageBundle = loadMessageBundle;
61+
function config(opts) {
62+
if (opts) {
63+
if (isString(opts.locale)) {
64+
options.locale = opts.locale.toLowerCase();
65+
options.language = options.locale;
66+
resolvedLanguage = undefined;
67+
resolvedBundles = Object.create(null);
68+
}
69+
if (opts.messageFormat !== undefined) {
70+
options.messageFormat = opts.messageFormat;
71+
}
72+
if (opts.bundleFormat === BundleFormat.standalone && options.languagePackSupport === true) {
73+
options.languagePackSupport = false;
74+
}
75+
}
76+
isPseudo = options.locale === 'pseudo';
77+
return loadMessageBundle;
78+
}
79+
exports.config = config;

extensions/markdown-language-features/src/markdownEngine.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as crypto from 'crypto';
7-
import * as path from 'path';
86
import { MarkdownIt, Token } from 'markdown-it';
7+
import * as path from 'path';
98
import * as vscode from 'vscode';
109
import { MarkdownContributionProvider as MarkdownContributionProvider } from './markdownExtensions';
1110
import { Slugifier } from './slugify';
1211
import { SkinnyTextDocument } from './tableOfContentsProvider';
13-
import { MarkdownFileExtensions, Schemes, isOfScheme } from './util/links';
12+
import { hash } from './util/hash';
13+
import { isOfScheme, MarkdownFileExtensions, Schemes } from './util/links';
1414

1515
const UNICODE_NEWLINE_REGEX = /\u2028|\u2029/g;
1616

@@ -197,9 +197,7 @@ export class MarkdownEngine {
197197

198198
const src = token.attrGet('src');
199199
if (src) {
200-
const hash = crypto.createHash('sha256');
201-
hash.update(src);
202-
const imgHash = hash.digest('hex');
200+
const imgHash = hash(src);
203201
token.attrSet('id', `image-hash-${imgHash}`);
204202
}
205203

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/**
7+
* Return a hash value for an object.
8+
*/
9+
export function hash(obj: any, hashVal = 0): number {
10+
switch (typeof obj) {
11+
case 'object':
12+
if (obj === null) {
13+
return numberHash(349, hashVal);
14+
} else if (Array.isArray(obj)) {
15+
return arrayHash(obj, hashVal);
16+
}
17+
return objectHash(obj, hashVal);
18+
case 'string':
19+
return stringHash(obj, hashVal);
20+
case 'boolean':
21+
return booleanHash(obj, hashVal);
22+
case 'number':
23+
return numberHash(obj, hashVal);
24+
case 'undefined':
25+
return 937 * 31;
26+
default:
27+
return numberHash(obj, 617);
28+
}
29+
}
30+
31+
function numberHash(val: number, initialHashVal: number): number {
32+
return (((initialHashVal << 5) - initialHashVal) + val) | 0; // hashVal * 31 + ch, keep as int32
33+
}
34+
35+
function booleanHash(b: boolean, initialHashVal: number): number {
36+
return numberHash(b ? 433 : 863, initialHashVal);
37+
}
38+
39+
function stringHash(s: string, hashVal: number) {
40+
hashVal = numberHash(149417, hashVal);
41+
for (let i = 0, length = s.length; i < length; i++) {
42+
hashVal = numberHash(s.charCodeAt(i), hashVal);
43+
}
44+
return hashVal;
45+
}
46+
47+
function arrayHash(arr: any[], initialHashVal: number): number {
48+
initialHashVal = numberHash(104579, initialHashVal);
49+
return arr.reduce((hashVal, item) => hash(item, hashVal), initialHashVal);
50+
}
51+
52+
function objectHash(obj: any, initialHashVal: number): number {
53+
initialHashVal = numberHash(181387, initialHashVal);
54+
return Object.keys(obj).sort().reduce((hashVal, key) => {
55+
hashVal = stringHash(key, hashVal);
56+
return hash(obj[key], hashVal);
57+
}, initialHashVal);
58+
}

0 commit comments

Comments
 (0)