Skip to content
gpdoc
Playground

Markdown

Import CommonMark into a gpdoc editor and export it back to Markdown.

Markdown converts between source text and a document described by your installed plugins. The conversion APIs return warnings for content they cannot represent.

Run this from a Quickstart project.

Terminal window
cargo add gpdoc-plugin-markdown

A blank line separates two paragraphs in Markdown. Use the Quickstart paragraph plugin to import both paragraphs and export them again.

use gpdoc::EditorConfig;
use gpdoc_plugin_markdown::{markdown, open, to_markdown};
use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(markdown())
.build()
.expect("configure editor");
let source = "First paragraph.\n\nSecond paragraph.";
let imported = open(&config, source)?;
assert_eq!(imported.value.document().blocks().len(), 2);
assert!(imported.warnings.is_empty());
let exported = to_markdown(&imported.value)?;
assert!(exported.warnings.is_empty());
assert_eq!(exported.value.trim(), source);

Pass an editor config and a Markdown string to open a new editor. Read the editor from the returned conversion’s value field. Missing plugins can produce warnings about formatting the config cannot represent.

let imported = open(&config, "First paragraph.")?;
let editor = imported.value;

Pass a borrowed editor to produce a Conversion<String, DocPos>. Export uses the Markdown rules in that editor’s config.

let exported = to_markdown(&editor)?;
let source = exported.value;

A Conversion<T, P> carries value: T and warnings: Vec<Warning<P>>. Each warning supplies a message and a position. Import positions are UTF-8 byte ranges in the input. Export positions are document positions. For example, importing raw HTML preserves it as text and reports a warning.

for warning in imported.warnings {
eprintln!("bytes {:?}: {}", warning.position, warning.message);
}

Register markdown() to add the command to an existing editor. Pass the required markdown: String argument. One transaction replaces the document and places the caret at its first available text position.

let mut editor = open(&config, "Draft")?.value;
editor.run(gpdoc_plugin_markdown::Set { markdown: "Published".into() })?;

Declare the mark name and delimiter with MarkdownSyntax::mark to support literal punctuation such as ==urgent==. Choose punctuation CommonMark delivers as ordinary inline text. Unmatched delimiters stay literal. Export warns if the delimiter cannot preserve mark attributes.

use gpdoc_plugin::{core::MarkSpec, Plugin};
use gpdoc_plugin_markdown::MarkdownSyntax;
let important: Plugin = Plugin::new("important", |p| {
p.mark(MarkSpec::new("important"));
p.metadata(MarkdownSyntax::default().mark("important", "=="));
});

Register a fence with MarkdownSyntax::fence(node, info, parse, render). The info string must be unique and contain only ASCII letters, digits, hyphens, or underscores. The parser receives the body without the final newline and returns a node of the registered kind. The renderer returns the body for export.

use gpdoc_plugin::{core::{Error, Node, NodeSpec}, Plugin};
use gpdoc_plugin_markdown::MarkdownSyntax;
fn notice(
parse: fn(&str) -> Result<Node, Error>,
render: fn(&Node) -> String,
) -> Plugin {
Plugin::new("notice", |p| {
p.node(NodeSpec::block("notice").group("block").content("text*"));
p.metadata(MarkdownSyntax::default().fence("notice", "notice", parse, render));
})
}