Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should add a spec loader', async () => {
let called = 0;
let loaded = 0;
Mode.addSpecLoader(async spec => {
called++;
if (spec.mode !== 'bar') {
return false;
}
loaded++;
return true;
}, 42);
CodeMirror.modeInfo.push(fakeMode('bar'));
let spec = await Mode.ensure('bar');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
expect(spec!.name).to.equal('BAR');
spec = await Mode.ensure('python');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
try {
spec = await Mode.ensure('APL');
} catch (err) {
// apparently one cannot use webpack `require` in jest
}
expect(called).to.equal(2);
expect(loaded).to.equal(1);
});
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { editorServices } from '@jupyterlab/codemirror';
import { CodeConsole, ConsolePanel } from '@jupyterlab/console';
import { defaultRenderMime } from '../../utils';
export const editorFactory = editorServices.factoryService.newInlineEditor.bind(
editorServices.factoryService
);
export const mimeTypeService = editorServices.mimeTypeService;
export const rendermime = defaultRenderMime();
/**
* Create a console content factory.
*/
export function createConsoleFactory(): CodeConsole.IContentFactory {
return new CodeConsole.ContentFactory({ editorFactory });
}
/**
* Create a panel content factory.
*/
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { editorServices } from '@jupyterlab/codemirror';
import { CodeConsole, ConsolePanel } from '@jupyterlab/console';
import { defaultRenderMime } from '../../utils';
export const editorFactory = editorServices.factoryService.newInlineEditor.bind(
editorServices.factoryService
);
export const mimeTypeService = editorServices.mimeTypeService;
export const rendermime = defaultRenderMime();
/**
* Create a console content factory.
*/
export function createConsoleFactory(): CodeConsole.IContentFactory {
return new CodeConsole.ContentFactory({ editorFactory });
}
/**
* Create a panel content factory.
called++;
if (spec.mode !== 'bar') {
return false;
}
loaded++;
return true;
}, 42);
CodeMirror.modeInfo.push(fakeMode('bar'));
let spec = await Mode.ensure('bar');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
expect(spec!.name).to.equal('BAR');
spec = await Mode.ensure('python');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
try {
spec = await Mode.ensure('APL');
} catch (err) {
// apparently one cannot use webpack `require` in jest
}
expect(called).to.equal(2);
expect(loaded).to.equal(1);
});
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { editorServices } from '@jupyterlab/codemirror';
import { CodeConsole, ConsolePanel } from '@jupyterlab/console';
import { defaultRenderMime } from '../../utils';
export const editorFactory = editorServices.factoryService.newInlineEditor.bind(
editorServices.factoryService
);
export const mimeTypeService = editorServices.mimeTypeService;
export const rendermime = defaultRenderMime();
/**
* Create a console content factory.
*/
export function createConsoleFactory(): CodeConsole.IContentFactory {
return new CodeConsole.ContentFactory({ editorFactory });
}
/**
* Create a panel content factory.
*/
export function createConsolePanelFactory(): ConsolePanel.IContentFactory {
return new ConsolePanel.ContentFactory({ editorFactory });
}
highlight: (code, lang, callback) => {
let cb = (err: Error | null, code: string) => {
if (callback) {
callback(err, code);
}
return code;
};
if (!lang) {
// no language, no highlight
return cb(null, code);
}
Mode.ensure(lang)
.then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
return cb(null, code);
}
try {
Mode.run(code, spec.mime, el);
return cb(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
return cb(err, code);
}
})
.catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
highlight: (code, lang, callback) => {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
callback(null, code);
return;
}
try {
Mode.run(code, spec.mime, el);
callback(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
callback(err, code);
}
}).catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
console.log(`Require CodeMirror mode error: ${err}`);
highlight: (code, lang, callback) => {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
callback(null, code);
return;
}
try {
Mode.run(code, spec.mime, el);
callback(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
callback(err, code);
}
}).catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
console.log(`Require CodeMirror mode error: ${err}`);
highlight: (code, lang, callback) => {
let cb = (err: Error | null, code: string) => {
if (callback) {
callback(err, code);
}
return code;
};
if (!lang) {
// no language, no highlight
return cb(null, code);
}
Mode.ensure(lang)
.then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
return cb(null, code);
}
try {
Mode.run(code, spec.mime, el);
return cb(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
return cb(err, code);
}
})
.catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
function initializeMarked(): void {
if (markedInitialized) {
return;
}
markedInitialized = true;
marked.setOptions({
gfm: true,
sanitize: false,
tables: true,
// breaks: true; We can't use GFM breaks as it causes problems with tables
langPrefix: `cm-s-${CodeMirrorEditor.defaultConfig.theme} language-`,
highlight: (code, lang, callback) => {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
callback(null, code);
return;