codemirror-ipythongfm.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import CodeMirror from 'codemirror';
  4. import 'codemirror/mode/stex/stex';
  5. import 'codemirror/mode/gfm/gfm';
  6. import 'codemirror/addon/mode/multiplex';
  7. /**
  8. * Define an IPython GFM (GitHub Flavored Markdown) mode.
  9. *
  10. * Is just a slightly altered GFM Mode with support for LaTeX.
  11. * LaTeX support was supported by Codemirror GFM as of
  12. * https://github.com/codemirror/CodeMirror/pull/567
  13. * But was later removed in
  14. * https://github.com/codemirror/CodeMirror/commit/d9c9f1b1ffe984aee41307f3e927f80d1f23590c
  15. */
  16. CodeMirror.defineMode(
  17. 'ipythongfm',
  18. (config: CodeMirror.EditorConfiguration, modeOptions?: any) => {
  19. let gfmMode = CodeMirror.getMode(config, {
  20. name: 'gfm',
  21. // Override list3 with an under-used token, rather than `keyword`
  22. tokenTypeOverrides: { list3: 'string-2' }
  23. });
  24. let texMode = CodeMirror.getMode(config, {
  25. name: 'stex',
  26. inMathMode: true
  27. });
  28. return CodeMirror.multiplexingMode(
  29. gfmMode,
  30. {
  31. open: '$$',
  32. close: '$$',
  33. mode: texMode,
  34. delimStyle: 'delimit'
  35. },
  36. {
  37. open: '$',
  38. close: '$',
  39. mode: texMode,
  40. delimStyle: 'delimit'
  41. },
  42. {
  43. open: '\\(',
  44. close: '\\)',
  45. mode: texMode,
  46. delimStyle: 'delimit'
  47. },
  48. {
  49. open: '\\[',
  50. close: '\\]',
  51. mode: texMode,
  52. delimStyle: 'delimit'
  53. }
  54. // .. more multiplexed styles can follow here
  55. );
  56. },
  57. 'gfm'
  58. );
  59. CodeMirror.defineMIME('text/x-ipythongfm', 'ipythongfm');
  60. CodeMirror.modeInfo.push({
  61. ext: [],
  62. mime: 'text/x-ipythongfm',
  63. mode: 'ipythongfm',
  64. name: 'ipythongfm'
  65. });