# What's the correct way to create a markdown cell programmatically?

**URL:** <https://discourse.jupyter.org/t/whats-the-correct-way-to-create-a-markdown-cell-programmatically/22527>\
**Category:** JupyterLab\
**Tags:** how-to, help-wanted, markdown\
**Created:** [November 21, 2023, 7:12pm UTC](https://discourse.jupyter.org/t/whats-the-correct-way-to-create-a-markdown-cell-programmatically/22527 "2023-11-21T19:12:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleksanderhan](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/aleksanderhan/32/10623_2.png) [@aleksanderhan](https://discourse.jupyter.org/u/aleksanderhan)\
**Post date:** [November 21, 2023, 7:12pm UTC](https://discourse.jupyter.org/t/whats-the-correct-way-to-create-a-markdown-cell-programmatically/22527/1 "2023-11-21T19:12:48Z")

</div>

I’m trying to write a Jupyter lab 4 extension and want to add a markdown cell, but when I do it renders as a raw cell in the ui. Am I doing something wrong. This is my code:

```auto
const model = notebookTracker.currentWidget.content.model;
const rendermime: IRenderMimeRegistry = notebookPanel.content.rendermime;
const contentFactory = notebookPanel.content.contentFactory;
const markdownCellModel = new MarkdownCellModel({});

const cell = contentFactory.createMarkdownCell({ model: markdownCellModel, rendermime, contentFactory });
model.sharedModel.insertCell(activeCellIndex + 1, cell);

```

---

<div class="post-metadata">

**Author:** ![jjvraw](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jjvraw/32/11474_2.png) [@jjvraw](https://discourse.jupyter.org/u/jjvraw)\
**Post date:** [November 28, 2023, 4:54pm UTC](https://discourse.jupyter.org/t/whats-the-correct-way-to-create-a-markdown-cell-programmatically/22527/2 "2023-11-28T16:54:06Z")

</div>

JupyterLab’s [shortcuts](https://blog.ja-ke.tech/assets/jupyterlab-shortcuts/Shortcuts.pdf), are also executable from an extension. You could begin with an insertion command `notebook:insert-cell-below`, then change it to a markdown cell `notebook:change-cell-to-markdown`.

Something like this:

```auto
... activate: (app: JupyterFrontEnd, notebookTracker: INotebookTracker) => {
  const addMarkdownCell = () => {
    const { commands } = app;
    const notebookPanel = notebookTracker.currentWidget;

    if (notebookPanel) {
      commands.execute('notebook:insert-cell-below').then(() => {
        commands.execute('notebook:change-cell-to-markdown');
      });
    }
  };

  addMarkdownCell();
}

```

Ran it now - seems to work.
