Document content
What the text of a chapter actually looks like when your plugin reads it, how to convert it, and how to write it back without damaging someone's manuscript.
On this page
The thing plugin authors get wrong over and over: what is in a chapter is not HTML and it is not plain text. It is ProseMirror JSON, the editor’s own way of describing a document, handed to you as a string.
const raw = await tintero.project.getFileContent(fileId);
typeof raw; // 'string'
JSON.parse(raw); // { type: 'doc', content: [ … ] }
getFileContent() and getDocContent() both give you that string, or null if there is
nothing in the file yet.
Converting
You almost never want the raw tree. convert.format gives you six converters, three in each direction:
const raw = await tintero.project.getFileContent(fileId);
const text = await tintero.convert.toText(raw);
const html = await tintero.convert.toHtml(raw);
const markdown = await tintero.convert.toMarkdown(raw);
const document = await tintero.convert.fromMarkdown('# Chapter one\n\nIt was a dark night.');
const fromHtml = await tintero.convert.fromHtml('<p>It was a dark night.</p>');
const fromText = await tintero.convert.fromText('It was a dark night.');
The to* converters take either the raw string or an object you already parsed, so you can hand
them whatever getFileContent() gave you. The from* converters give you back an object.
Writing back
Here is where it catches people:
const document = await tintero.convert.fromMarkdown('# New chapter\n\nHello.');
await tintero.project.updateFileContent(fileId, JSON.stringify(document));
// ^^^^^^^^^^^^^^^^^^^^^^^^
// a string, not the object
fromMarkdown() hands you an object; updateFileContent() wants a string. Forgetting that
JSON.stringify is the most common mistake anyone makes with this API.
Before your write lands, the text it displaces (what is open in the editor, or otherwise what is on disk) goes into that chapter’s local history, where the writer can compare it and bring it back. That copy never syncs, and if it cannot be saved, your write does not happen. The one gap: a file on disk that cannot be read at all has nothing to copy, and is replaced as it is.
Permissions: rewriting is not creating
The read side splits the same way. project.read.files gets you the list of files and folders; project.read.fileContent gets you what is inside them. A plugin that only needs an outline never has to ask for the words.
Adding to the end
To add to a chapter, do not read it and write it all back. appendFileContent() and
appendDocContent() take a ProseMirror document, the same string updateFileContent() wants,
and add its blocks after whatever is already there:
async function appendParagraph(fileId, sentence) {
const addition = await tintero.convert.fromText(sentence);
await tintero.project.appendFileContent(fileId, JSON.stringify(addition));
}
Only what you send goes through a converter. The text already in the chapter is never turned into anything else and back, so nothing in it can be lost on the way. The chapter does not have to be open. If it is, the addition goes after what is in the editor, unsaved changes included, not after the copy on disk. This is also how to write something long: in parts, one call each, rather than in one huge string.
It goes through the same cleaning and the same local-history copy as updateFileContent(), with
one more rule: what you send must have doc as its root, or the call fails before anything is
read.
Changing a phrase
To change a few words, editor.replaceText() finds a literal piece of text and replaces only that,
in any chapter or document, open or not:
const result = await tintero.editor.replaceText({
fileId,
find: 'Mr. Darcy',
replace: 'Mr. Bingley',
expectedCount: 3,
});
result.replaced; // 0 if there were not exactly three; nothing was changed
result.occurrences; // where each one is: { blockIndex, offset, text, context }
The match is case-sensitive unless you pass matchCase: false. It can cross formatting (a word in
bold halfway through still matches) but never a paragraph break, and the new text takes the
formatting of the first matched character. An empty replace deletes. Leave out expectedCount
and every occurrence is replaced. If there are none at all, the call rejects.
annotate: true leaves a note with the old text on each replacement, in your plugin’s
annotation layer.
project.searchProse() returns the same occurrences for each hit, so you can search first,
show the writer what you found, and replace afterwards.
Changing everything else
For anything else, read the document, change it and write it back. The easy way is through Markdown:
async function shoutHeadings(fileId) {
const raw = await tintero.project.getFileContent(fileId);
const markdown = await tintero.convert.toMarkdown(raw);
const updated = await tintero.convert.fromMarkdown(
markdown.replace(/^(#+ .*)$/gm, (line) => line.toUpperCase()),
);
await tintero.project.updateFileContent(fileId, JSON.stringify(updated));
}
Going out to Markdown and back loses anything Markdown cannot express, which in a Tintero document is a great deal: scenes, bookmarks, character highlights, screenplay formatting and inline audio have no Markdown to be turned into. Fine when you are moving text around, wrong when you need to keep every bit of formatting. For that, walk the ProseMirror tree yourself:
async function countParagraphs(fileId) {
const raw = await tintero.project.getFileContent(fileId);
if (!raw) return 0;
const document = JSON.parse(raw);
return (document.content || []).filter((node) => node.type === 'paragraph').length;
}
The node names you can meet while walking are the
document schema, and
how ProseMirror documents work covers the shape in full: why
formatting is a marks array on a text node rather than a wrapper, and why an unknown node
type should be passed through rather than dropped.
Editor content versus file content
These are two different things and they can disagree:
tintero.projectreads what is saved on disk.tintero.editorreads what is on screen right now, unsaved changes included.
If someone is halfway through a sentence, editor.getWordCount() counts it and
project.getFileContent() does not. Use the editor calls and the editor.activeDocumentChanged
event for anything live, and the project calls for going through the whole manuscript.
The editor’s write methods take HTML, not ProseMirror JSON, because they work on the live
editor rather than on a stored file. That goes for editor.insertAt(), editor.replaceRange()
and editor.replaceSelection().
await tintero.editor.replaceSelection('<strong>replaced</strong>');
Images
project.getImages() tells you what images exist. project.getImageData() gives you a base64
data URL you can drop straight into an src. It works that way because your frame is not allowed
to load files off disk, so the app has to hand you the bytes themselves.
const images = await tintero.project.getImages();
const dataUrl = await tintero.project.getImageData(images[0].fileName);
document.getElementById('preview').src = dataUrl;