How to use @jupyterlab/codemirror - 10 common examples

To help you get started, we’ve selected a few @jupyterlab/codemirror examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
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);
    });
github jupyterlab / jupyterlab / tests / test-console / src / utils.ts View on Github external
// 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.
 */
github jupyterlab / jupyterlab / tests / test-console / src / utils.ts View on Github external
// 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.
github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
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);
    });
github jupyterlab / jupyterlab / tests / test-console / src / utils.ts View on Github external
// 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 });
}
github yuvipanda / simplest-notebook / packages / rendermime / src / renderers.ts View on Github external
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}`);
github jupyterlab / jupyterlab / packages / rendermime / src / widgets.ts View on Github external
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}`);
github jupyterlab / jupyterlab / packages / rendermime / src / renderers.ts View on Github external
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}`);
github jupyterlab / jupyterlab-data-explorer / packages / rendermime / src / renderers.ts View on Github external
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}`);
github jupyterlab / jupyterlab / packages / rendermime / src / renderers.ts View on Github external
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;