Skip to main content

Saving Markdown with GoTipe: keep the source snapshot

Updated 4 min readMarkdown

Our fixture was a release note that had two tasks, a heading, and a YAML field naming the Docs team in its front matter; we opened it in GoTipe's headless JavaScript editor, checked one task, saved it, unchecked the task and saved it again. GoTipe is a block editor library for apps that store Markdown.

1. The team is outside the document

The parser outputs a document and a snapshot of the source; the snapshot contains a field team: "Docs", which will be removed if you serialize the edited document without the snapshot; the heading will change from an underlined Setext heading to # Release notes.

This control is a string stored in memory; the test script checks the value without writing to disk. Storing only the JSON of the editor document would leave the saving routine without the original front matter.

2. Three spaces still mean three spaces

The first file saved should be exactly the same as the original except for the substitution [ ] to [x] at 'Review the résumé'. The three spaces in 'Keep this spacing.', the accents, the byte order mark, and the Windows CRLF line endings are also included in the buffer comparison.

These details are in the actual UTF-8 files, not the terminal or Markdown viewer representation; any change to an unmodified byte would cause the assertion to fail, regardless of how similar the output looked.

3. A snapshot for the next save

Through a separate public command uncheck the task and pass in first.snapshot to the serializer. The second output file will be identical to the original bytes. This is the session you want. Serialize the current document, keep the snapshot that was returned, and use that for the next save. PageEditor takes care of that for you.

To execute the fixture, you'll have to install the packages in a fresh directory, save the script as snapshot-check.mjs, and then run node snapshot-check.mjs. The script will delete the temporary files it creates.

npm init -y
npm install --save-exact @gotipe/markdown@0.1.3 @gotipe/core@0.1.6
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createEditor } from "@gotipe/core/js";
import { Heading, Paragraph, TodoList } from "@gotipe/core/extensions";
import { parseMarkdown, serializeMarkdown } from "@gotipe/markdown";

const original = "\uFEFF" + [
  "---", 'team: "Docs"', "---", "", "Release notes", "=============", "",
  "Keep   this spacing.", "", "- [ ] Review the résumé", "- [x] Package the build", "",
].join("\r\n");
const directory = mkdtempSync(join(tmpdir(), "gotipe-snapshot-"));
let editor;
try {
  const input = join(directory, "release.md");
  const checkedFile = join(directory, "release-checked.md");
  const resetFile = join(directory, "release-reset.md");
  writeFileSync(input, original, { flag: "wx" });
  const { doc, snapshot } = parseMarkdown(readFileSync(input, "utf8"));
  editor = await createEditor({
    extensions: [Paragraph, Heading, TodoList], content: doc,
  });
  const list = doc.blocks.find(block => block.type === "todoList");
  assert.ok(list);
  const task = list.children[0];
  assert.equal(task.content[0].text, "Review the résumé");
  assert.equal(task.attrs.checked, false);

  assert.equal(editor.chain().setBlockAttrs(task.id, { checked: true }).run(), true);
  const first = serializeMarkdown(editor.getJSON(), snapshot);
  writeFileSync(checkedFile, first.text, { flag: "wx" });
  const expected = original.replace("- [ ] Review", "- [x] Review");
  assert.deepEqual(readFileSync(checkedFile), Buffer.from(expected, "utf8"));

  const withoutSnapshot = serializeMarkdown(editor.getJSON()).text;
  assert.notEqual(withoutSnapshot, first.text);
  assert.equal(withoutSnapshot.includes('team: "Docs"'), false);
  assert.ok(withoutSnapshot.includes("# Release notes"));

  assert.equal(editor.chain().setBlockAttrs(task.id, { checked: false }).run(), true);
  const second = serializeMarkdown(editor.getJSON(), first.snapshot);
  writeFileSync(resetFile, second.text, { flag: "wx" });
  assert.deepEqual(readFileSync(resetFile), readFileSync(input));
  console.log("First save: only the task marker changed.");
  console.log("BOM, front matter, Setext heading, spacing and CRLF matched.");
  console.log("Without a snapshot: front matter missing; heading becomes ATX.");
  console.log("Second save with the returned snapshot: original bytes restored.");
} finally {
  editor?.destroy();
  rmSync(directory, { recursive: true, force: true });
}

The three extensions apply to this fixture; other document structures will require their own extensions and tests. Changed blocks may be normalized. There is only one writer here; a save in the production environment still requires a revision check at the application level for concurrent changes.

See the Markdown snapshot contract and file-editing guide for integration details.

The Markdown codec and core packages used here are MIT-licensed. GoTipe's ready-made editor UI and themes use commercial licenses. This post was drafted with AI. The example was executed against the pinned package versions above.

About the author

  • GoTipe is a block editor for apps that store Markdown. Our TypeScript and Rust/WASM engines, Markdown codec and headless adapters for React, Vue, Svelte and Solid are MIT-licensed. The ready-made UI and themes use a commercial license. We share runnable examples of Markdown preservation, editor integration, collaboration and AI review. Try the editor and read the docs: https://gotipe.com Source and examples: https://github.com/veniameen/gotipe

No comments yet. Be the first to comment.