Function Repository Resource:

ExportMarkdownString

Source Notebook

Convert an expression to a string in Markdown format

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ExportMarkdownString"][expr]

converts expr to a string in Markdown format.

Details and Options

ResourceFunction["ExportMarkdownString"] has the following options:
ImageExportMethodNonethe method used to export images
ConversionRules{}a list of rules to apply to low-level box structures

Examples

Basic Examples (6) 

Convert an expression to markdown:

In[1]:=
ResourceFunction["ExportMarkdownString"][
 Row[{Style["bold", FontWeight -> Bold], " and ", Style["italic", FontSlant -> Italic], " text"}]]
Out[1]=

Create a Markdown table:

In[2]:=
ResourceFunction["ExportMarkdownString"][
 TableForm[{{a, b}, {c, d}}, TableHeadings -> {None, {"First", "Second"}}]]
Out[2]=

Include a code block and TEX expressions:

In[3]:=
ResourceFunction["ExportMarkdownString"][
 Row[{"This can format ", Style[TraditionalForm, "InlineCode"], " too: ", TraditionalForm[Integrate[Sin[x]/Log[x], x]]}]]
Out[3]=

Convert cells:

In[4]:=
ResourceFunction["ExportMarkdownString"][
 TextCell["Hello world", "Title"]]
Out[4]=
In[5]:=
ResourceFunction["ExportMarkdownString"][
 TextCell["Cool Stuff", "Section"]]
Out[5]=
In[6]:=
ResourceFunction["ExportMarkdownString"][
 ExpressionCell[Defer[1 + 1], "Input"]]
Out[6]=

A CellGroup:

In[7]:=
ResourceFunction["ExportMarkdownString"][CellGroup[{
   TextCell[
    Row[{"Format math using ", Style[TraditionalForm, "InlineCode"]}],
     "Text"],
   ExpressionCell[TraditionalForm[Integrate[Sin[Sin[x]], {x, 0, 1}]]]
   }]]
Out[7]=

Convert a notebook:

In[8]:=
StringTake[
 ResourceFunction["ExportMarkdownString"][
  Import["ExampleData/document.nb"]], UpTo[250]]
Out[8]=

Scope (5) 

Cells (3) 

Convert a CellObject:

In[9]:=
ResourceFunction["ExportMarkdownString"][EvaluationCell[]]
Out[9]=

A Cell with BoxData:

In[10]:=
ResourceFunction["ExportMarkdownString"][
 Cell[BoxData[RowBox[{"1", "+", "1"}]], "Input", CellLabel -> "In[1]:="]]
Out[10]=

Define a Cell with TextData:

In[11]:=
cell = Cell[
   TextData[{"A ", Cell[BoxData["TextData"], "InlineCode"], " cell with a ", ButtonBox["link", BaseStyle -> "Hyperlink", ButtonData -> {URL["https://www.wolfram.com"], None}, ButtonNote -> "https://www.wolfram.com"], " and ", StyleBox["styled", FontSlant -> "Italic"], " ", StyleBox["text", FontWeight -> "Bold"], "."}], "Text"];

See the cell:

In[12]:=
DisplayForm[cell]
Out[12]=

Export it to Markdown:

In[13]:=
ResourceFunction["ExportMarkdownString"][cell]
Out[13]=

External language cells:

In[14]:=
ResourceFunction["ExportMarkdownString"][
 Cell["print('hello world')", "ExternalLanguage", CellEvaluationLanguage -> "Python"]]
Out[14]=

Notebooks (2) 

Convert a NotebookObject:

In[15]:=
nbo = NotebookPut[Import["ExampleData/document.nb"]]
Out[15]=
In[16]:=
StringTake[ResourceFunction["ExportMarkdownString"][nbo], UpTo[250]]
Out[16]=
In[17]:=
NotebookClose[nbo]

Symbolic representations of notebooks:

In[18]:=
notebook = DocumentNotebook[{
   TextCell["Title", "Title"],
   TextCell["Stuff", "Section"],
   TextCell["Here is some stuff:", "Text"],
   ExpressionCell[Defer[1 + 1], "Input", CellLabel -> "In[1]:="],
   ExpressionCell[2, "Output", CellLabel -> "Out[1]="],
   TextCell[
    Row[{"A ", Hyperlink["link", "https://www.wolfram.com"], " in text."}], "Text"],
   TraditionalForm[Limit[f[x], x -> a]],
   TextCell["Other Stuff", "Section"],
   TextCell["Items", "Subsection"],
   TextCell["First", "Item"],
   TextCell["Second", "Item"],
   TextCell["Third (subitem)", "Subitem"],
   TextCell["Formatting", "Subsection"],
   TextCell[
    Row[{Style["This text", FontWeight -> Bold], " is bold."}], "Text"],
   TextCell[
    Row[{Style["This text", FontSlant -> Italic], " is italic."}], "Text"],
   TextCell[
    Row[{Style["This text", FontWeight -> Bold, FontSlant -> Italic], " is both."}], "Text"],
   TextCell["Tables", "Subsubsection"],
   TableForm[{{a, b, c}, {d, e, f}}, TableHeadings -> {None, {"First", "Second", "Third"}}],
   Grid[Table[RandomWord[3], 5], Alignment -> {{Left, Center, Right}, Automatic}]
   }]
Out[18]=
In[19]:=
ResourceFunction["ExportMarkdownString"][notebook]
Out[19]=

Options (3) 

ImageExportMethod (3) 

By default, images in notebooks will be represented as snippets of their box representations:

In[20]:=
notebook = DocumentNotebook[{TextCell["Look at this cat:", "Text"], Entity["TaxonomicSpecies", "FelisCatus::ddvt3"][
    EntityProperty["TaxonomicSpecies", "Image"]]}]
Out[20]=
In[21]:=
ResourceFunction["ExportMarkdownString"][notebook]
Out[21]=

Specify an image export method to create markdown images:

In[22]:=
ResourceFunction["ExportMarkdownString"][notebook, "ImageExportMethod" -> "CloudObject"]
Out[22]=

Specify a directory for exporting images:

In[23]:=
images = CreateDirectory[]
Out[23]=
In[24]:=
ResourceFunction["ExportMarkdownString"][DocumentNotebook[{
   TextCell["Look at this cat:"],
   Entity["TaxonomicSpecies", "FelisCatus::ddvt3"][
    EntityProperty["TaxonomicSpecies", "Image"]]
   }], "ImageExportMethod" -> File[images]]
Out[24]=

Images are saved in the given directory:

In[25]:=
FileNames[All, images]
Out[25]=
In[26]:=
Import /@ %
Out[26]=

Specify a custom function:

In[27]:=
ResourceFunction["ExportMarkdownString"][DocumentNotebook[{
   TextCell["Look at this cat:"],
   Entity["TaxonomicSpecies", "FelisCatus::ddvt3"][
    EntityProperty["TaxonomicSpecies", "Image"]]
   }], "ImageExportMethod" -> Function["(image removed)"]]
Out[27]=

Applications (1) 

Give a notebook as context to an LLM:

In[28]:=
markdown = ResourceFunction["ExportMarkdownString"][EvaluationNotebook[]];
In[29]:=
LLMSynthesize[{"What do you think of my notebook? What should I add next?", markdown}]
Out[29]=

Properties and Relations (1) 

For many simple markdown strings, ExportMarkdownString and ImportMarkdownString are effectively inverses of one another:

In[30]:=
ResourceFunction["ExportMarkdownString"][
 ResourceFunction["ImportMarkdownString"][
  "**bold** and *italic* text"]]
Out[30]=

Possible Issues (2) 

ExportMarkdownString uses a set of heuristics to determine when to show string characters:

In[31]:=
ResourceFunction["ExportMarkdownString"][f["abc"]]
Out[31]=
In[32]:=
ResourceFunction["ExportMarkdownString"][
 ExpressionCell[f["abc"], "Input"]]
Out[32]=

Explicitly control this behavior using the ShowStringCharacters option with Style in the appropriate place:

In[33]:=
ResourceFunction["ExportMarkdownString"][
 Style[f["abc"], ShowStringCharacters -> True]]
Out[33]=
In[34]:=
ResourceFunction["ExportMarkdownString"][
 ExpressionCell[Style[f["abc"], ShowStringCharacters -> False], "Input"]]
Out[34]=

Use Defer instead of Unevaluated to prevent evaluation of arguments:

In[35]:=
ResourceFunction["ExportMarkdownString"][
 Unevaluated[Table[i^2, {i, 10}]]]
Out[35]=
In[36]:=
ResourceFunction["ExportMarkdownString"][Defer[Table[i^2, {i, 10}]]]
Out[36]=

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 23 August 2024

Related Resources

License Information