index.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { JupyterLab, JupyterLabPlugin } from '@jupyterlab/application';
  6. import { IDocumentManager } from '@jupyterlab/docmanager';
  7. import {
  8. ILatexTypesetter,
  9. IRenderMimeRegistry,
  10. RenderMimeRegistry,
  11. standardRendererFactories
  12. } from '@jupyterlab/rendermime';
  13. namespace CommandIDs {
  14. export const handleLink = 'rendermime:handle-local-link';
  15. }
  16. /**
  17. * A plugin providing a rendermime registry.
  18. */
  19. const plugin: JupyterLabPlugin<RenderMimeRegistry> = {
  20. id: '@jupyterlab/rendermime-extension:plugin',
  21. requires: [IDocumentManager],
  22. optional: [ILatexTypesetter],
  23. provides: IRenderMimeRegistry,
  24. activate: activate,
  25. autoStart: true
  26. };
  27. /**
  28. * Export the plugin as default.
  29. */
  30. export default plugin;
  31. /**
  32. * Activate the rendermine plugin.
  33. */
  34. function activate(
  35. app: JupyterLab,
  36. docManager: IDocumentManager,
  37. latexTypesetter: ILatexTypesetter | null
  38. ) {
  39. app.commands.addCommand(CommandIDs.handleLink, {
  40. label: 'Handle Local Link',
  41. execute: args => {
  42. const path = args['path'] as string | undefined | null;
  43. const id = args['id'] as string | undefined | null;
  44. if (!path) {
  45. return;
  46. }
  47. // First check if the path exists on the server.
  48. return docManager.services.contents
  49. .get(path, { content: false })
  50. .then(() => {
  51. // Open the link with the default rendered widget factory,
  52. // if applicable.
  53. const factory = docManager.registry.defaultRenderedWidgetFactory(
  54. path
  55. );
  56. const widget = docManager.openOrReveal(path, factory.name);
  57. if (!widget) {
  58. return;
  59. }
  60. return widget.revealed.then(() => {
  61. // Once the widget is ready, attempt to scroll the hash into view
  62. // if one has been provided.
  63. if (!id) {
  64. return;
  65. }
  66. // Look for the an element with the hash id in the document.
  67. // This id is set automatically for headers tags when
  68. // we render markdown.
  69. const element = widget.node.querySelector(id);
  70. if (element) {
  71. element.scrollIntoView();
  72. }
  73. return;
  74. });
  75. });
  76. }
  77. });
  78. return new RenderMimeRegistry({
  79. initialFactories: standardRendererFactories,
  80. linkHandler: {
  81. handleLink: (node: HTMLElement, path: string, id?: string) => {
  82. app.commandLinker.connectNode(node, CommandIDs.handleLink, {
  83. path,
  84. id
  85. });
  86. }
  87. },
  88. latexTypesetter
  89. });
  90. }