Import
Folio opens Word, OpenDocument, RTF, HTML, Markdown, LaTeX, plain text, EPUB and PDF files. Importing has three layers, so a host only pays for what it uses:
| Layer | Package | What it does |
|---|---|---|
| Contract | @nextgensoftwares/folio-import | Importer, ImportResult, importFile (pick the importer that claims a file), acceptOf, WarningLog, fitToSchema, and the shared XML reader @nextgensoftwares/folio-import/xml |
| Formats | @nextgensoftwares/folio-docx (Word) and @nextgensoftwares/folio-import-formats (everything else) | one importer per format, one entry per format (@nextgensoftwares/folio-import-formats/markdown, /html, /text, /odt, /rtf, /epub, /pdf, /latex) |
| Applying + UI | @nextgensoftwares/folio-plugin-import | applyImport (replace or insert, one undo step, progressive), toolbar items, drop handling, progress and warnings dialog |
Importers are DOM-free: they run in the browser, in a worker and in Node (a server can convert uploads with the same code). They never produce markup the editor would execute: scripts, styles, event handlers, frames and unsafe URLs are dropped, and only known node types with validated attributes come out.
Quick start
import { importFile, acceptOf } from '@nextgensoftwares/folio-import';
import { allImporters } from '@nextgensoftwares/folio-import-formats';
import { docxImporter } from '@nextgensoftwares/folio-docx';
import { applyImport, assetUploaderFrom } from '@nextgensoftwares/folio-plugin-import';
const importers = [docxImporter, ...allImporters()]; // stricter (magic-byte) importers first
const result = await importFile({ bytes, name: file.name, type: file.type }, importers, {
signal: controller.signal,
onProgress: (f) => bar.value = f,
});
// result.doc, result.page, result.theme, result.meta, result.comments, result.assets, result.warnings
await applyImport(editor, result, {
mode: 'replace', // or 'insert' at the caret, 'append' after the last block
theme, setTheme, // page size/margins and text size go through the host's theme
upload: assetUploaderFrom(mediaUploader), // where images go (else data: URLs)
comments: { store, actor }, // plugin-comments' store
});allImporters() returns lazy importers: sniffing a file and building the file picker's accept cost nothing, and a format's parser is loaded (import()) the first time a file of that format comes in. Hosts that offer only some formats import those entries directly:
import { markdownImporter } from '@nextgensoftwares/folio-import-formats/markdown';
import { htmlImporter } from '@nextgensoftwares/folio-import-formats/html';
const importers = [htmlImporter, markdownImporter];The import UI
import { importPlugin, assetUploaderFrom } from '@nextgensoftwares/folio-plugin-import';
const importer = importPlugin({
importers: () => [docxImporter, ...allImporters()],
// "Open file…" / "Open as a new document": the host makes a new document.
openDocument: (prepared) => openAsNewDocument(prepared.doc, prepared.theme, prepared.comments),
// Where "Import…" puts the file: 'new-document' (default), 'end' or 'caret'.
placement: () => settings.importPlacement,
// Settings for applying into the current editor ("Import…" at the end/caret, "Insert here").
applyOptions: () => ({ theme: currentTheme, setTheme, upload: assetUploaderFrom(uploader), comments: { store, actor } }),
// Offer "Attach as file" for dropped documents (plugin-media keeps doing this for everything else).
attach: (editor, files, at) => media.input.insertFiles(editor, files, at),
// Schema additions importers rely on: DOCX bookmark ids, so internal links survive.
schema: docxImportSchema,
});
const plugins = [importer, media /* after import: it sees the drops import declines */];Toolbar: "Open file…" (
import.open) and "Import…" (import.insert), groupfile. Route them anywhere like any item (a File menu, the mobile bar):route: { 'import.*': ['fileMenu'] }.Where "Import…" puts the file (
placement, a value or a getter read per import, so a host setting changes it live):new-document(default): opens it as a new document, the same path as "Open file…" (openDocument); the current document is left alone;end: inserts it after the document's last block (replacing a trailing empty paragraph), wherever the caret is, and moves the caret there;caret: inserts it at the caret, like Word's "Insert ▸ Text from File".
Inserted files keep their page setup as sections (below), so with a different page size the content's pages after the current one take the file's size.
new-documentneedsopenDocument: without it a new document can't be made (opening would replace this one), so the file goes in at theendinstead.importer.placement()says what "Import…" will do now.Drop: dropping an importable file (by extension or MIME type) on a page asks Open as a new document / Insert here / Attach as file. Images, video, audio and unknown files are not touched: plugin-media still turns them into media or file cards.
drop: falseturns the question off.Progress: the same dialog exports use (
runExport, drawn by<FolioView>): "Opening “a.docx”" / "Importing “a.docx”" with each step the importer names ("Reading the file…", "Loading fonts…", "Converting…", "Converting images 3/12", "Storing images 3/12", "Adding to the document"), a bar and Cancel (Escape too), which works at every step. Cancelling while applying reverts what was inserted. Importers name their steps throughonProgress(fraction, stage).Warnings: "Imported with 3 notes", expandable to the list of what couldn't be carried over (de-duplicated, with counts).
Without openDocument, "Open file…" replaces the current document in place, as one undo step.
Applying a result
applyImport(editor, result, options):
- stores assets the importer didn't upload (
upload, a few at a time; without one they becomedata:URLs; failures drop the image with a warning) and rewrites the document'sasset:refs; - fits the document to the editor's schema (
fitToSchema): node types the host lacks are degraded (callouts → quotes, unknown containers unwrapped, fields → text), unknown marks and attributes removed, invalid values reset; every loss is a warning; - replaces the document or inserts at the selection (
append: after the last block) withapplyProgressively: batches the page can paint between, one undo step, cancel reverts. In replace mode the document attributes travel too (header/footer settings, chapters). A file of several blocks goes in as whole top-level blocks, keeping their own direction, alignment and spacing, and never splits the caret's paragraph, list or table: an empty top-level paragraph is replaced, at the start of a top-level paragraph it goes before it, anywhere else after the top-level block holding the caret. A single paragraph of text merges at the caret like a paste; - calls
setThemewith the import's page size and margins and its theme hint: body size, paragraph spacing, line height, heading, list, table and quote styles (replace mode by default;applyPage,adoptTheme,adoptTextSize; fonts only withadoptFonts, since the host may not have them). Insert mode never changes the host theme. Withformatting: 'source'(the default), the look the file's theme gave the content is written into the inserted blocks wherever the host theme would draw it differently: heading and body sizes, weights, colours, line height, paragraph spacing, direction, the line model (a Word file'sleading: 'below'on each paragraph and heading, also in lists, cells and headers/footers, when the host'stheme.leadingdiffers), line breaking (lineBreaks: 'office'when the host'stheme.lineBreaksdiffers) and the math size (textSizeon each equation when the host'stheme.math.textSizediffers), and fonts withadoptFonts. Inserted content lays out line for line as when the file is opened. This is Word's "Keep Source Formatting".formatting: 'destination'lets the content take this document's styles instead. List indents and table cell padding still follow the host;- Page setup and headers/footers on insert. With
formatting: 'source', when the document can store sections (the header & footer plugin's schema), the file brings its setup along as Word's "Insert ▸ Text from File" brings its sections: the inserted content starts a section with the file's page size, margins, header/footer bands, columns, headers, footers, first-page and page-number settings, and the file's own section breaks follow under fresh ids (they never collide with this document's). The host content after it starts a section that restores exactly what it showed before (page, columns, every header/footer kind, number format, distances); a page in the middle of a host section doesn't become a "first page".sectionStartsays where they start:'continuous'(default, Word's continuous section break) the content continues on the current page, which keeps this document's page setup and headers/footers; the file's take effect from the next page (layout'sSectionSettings.deferPage), and likewise the host content after it continues on the file's last page, its own setup returning on the page after.'newPage': the content and the host content after it each start a new page. Inserted at the very start of the document, the file's setup becomes the document level instead (no page break before it). A file without headers/footers of its own keeps its page and shows this document's. Everything, the content and the settings, is one undo step. Passtheme(the page is restored from it) and, if the document may have no settings yet while the host draws its own zones,headerFooterSeed(the header & footer plugin'sseed, e.g.() => settingsFromConfig(zones)) so those pages keep them. Inserted inside a paragraph, list or table, withformatting: 'destination'or without section support, the file's setup is left out as before (warningssections-insert/header-footer-insert/header-footer-unsupported). Theme-wide settings stay this document's: odd/even pages, widows/orphans;
- Page setup and headers/footers on insert. With
- creates comment threads in the store as the importing user, with the file's author and date as each comment's
origin(see Comments ▸ Imported comments): cards show and exports write the original author, and the text stays as written.
prepareImport does steps 1, 2 and 4 without an editor, for hosts that open imports as new documents (create the editor from prepared.doc).
Limits and safety
Every importer takes opts.limits (maxBytes, maxEntries for archives, maxBlocks), signal and onProgress, and yields to the event loop every ~30 ms on big files. Archives are read with caps per entry and in total (zip-bomb guard); XML is parsed without DTDs or entity expansion (no XXE, no "billion laughs"); images must be real PNG/JPEG/GIF/WebP/BMP (SVG is never imported as an image); links keep only http(s), mailto and tel.
| Format | Default maxBytes | Other caps |
|---|---|---|
| HTML | 100 MB | 2,000 zip entries (Google Docs zip) |
| Markdown | 50 MB | |
| LaTeX | 200 MB | 5,000 zip entries, 16 levels of \input |
| Plain text | 100 MB | |
| ODT | 200 MB | 5,000 zip entries |
| RTF | 100 MB | 1,000 nested groups |
| EPUB | 200 MB | 10,000 zip entries |
| 200 MB | maxPages 2,000, 500 images |
All formats: maxBlocks 500,000 (the rest is cut with a warning).
Word (.docx)
@nextgensoftwares/folio-docx (docxImporter) reads .docx, .dotx, .docm and .dotm (macros are never read). Its goal is a book written in Word laying out in Folio as close to Word as possible. Word isn't available on Linux, so LibreOffice is the reference renderer for the fidelity numbers below.
- Styles: the full cascade (document defaults → table style → paragraph style chain → numbering level → direct formatting; character styles flip toggle properties). Normal and Heading 1–6 become
result.theme(fonts with metric-compatible fallbacks, sizes, colours, line height, paragraph spacing, heading spacing and keep-with-next). The same theme, with the first section's page setup, is kept in the document asdoc.attrs.theme(docTheme, default true;docTheme: falseleaves it out): layout merges it over the host's theme for that document wherever it is laid out (see A document's own theme), so the file paginates like Word even where the host lays out with its own theme. Load the fonts it names (or their metric-compatible substitutes, Liberation, Carlito, Caladea) for Word's line breaks. Paragraphs and runs carry only what differs: alignment, direction, line height, indents (negative ones reach into the margin), tab stops (w:tabsthrough the style cascade, clears and leaders;w:tabruns stay\t;defaultTabStop→theme.tabInterval; bar tabs are dropped with a warning),spaceBefore/spaceAfter, keep/page-break controls, and bold/italic/underline/strike/ sub/sup/code/colour/size/family/highlight marks, plus all caps, small caps and character spacing (w:caps,w:smallCaps,w:spacing→ textStyletextTransform/fontVariant/letterSpacing; the text stays as typed), and character scale (w:w, through run, character and paragraph styles too → textStylecharScale, clamped to 1–600, written only where it isn't 100; the DOCX export writes it back asw:w). Complex-script text (Arabic, Hebrew, Indic, Thai, or runs markedw:rtl/w:cs) takes onlyw:bCs/w:iCs/w:szCs, as in Word and LibreOffice, and runs that mix scripts are split by script. A run that is regular or upright in a bold or italic heading gets textStylefontWeight/fontStyle. Outline levels → headings (set on a paragraph only →visualHeading), Title/Subtitle stay styled paragraphs, quote styles → blockquotes, code styles and monospace paragraphs → code blocks. - Lists:
numbering.xmllevels, overrides, starts, formats, restart and continue (instances of one definition share counters); flat numbered paragraphs become nested lists (level, then indent), interrupted lists continue throughstart. Numbered headings keep their number as text. Each level's marker becomes the list'slistLevel: bullet characters in their font, colour and size (Symbol/Wingdings mapped to Unicode), picture bullets (numPicBullet, as image assets),lvlTexttemplates ("1.1.", "Chapter 1:", "(a)", "1-") with each level's format,lvlJc, and the level's indent/hanging when they differ from the theme's. Levels outside the list's own nesting (e.g. a numbered heading's "2" in "2.1") are baked into the template. Still approximated (warning): number formats with no Folio equivalent (CJK counting, Hebrew...) as decimal, symbol-font characters with no known Unicode equivalent (kept in their font). - Tables: grid widths (
colwidth),gridSpan,vMerge, cell shading, repeated header rows, nested tables; table width (tblW: twips, percent, auto → the grid's width), alignment (jc) and indent (tblInd); table and cell borders (tblBorders/tcBorders, the table style chain's too: single, double, dashed, dotted, none, widths and colours; borderless tables stay borderless); table styles' conditional formatting (tblStylePrheader row, last row, first/last column, banded rows/columns pertblLook) resolved into cell fills, borders and run formatting; cell margins (tblCellMar/tcMar) and vertical alignment (vAlign). Fancy border styles (wave, 3-D, thin-thick) become the closest line (warning). - Pictures: inline and floating DrawingML/VML pictures with Word's wrap (square, tight → square, top-and-bottom, behind, in front), side, distances and offsets → plugin-media placement. EMF/WMF/TIFF go through
convertImageor stay assets with a placeholder. Embedded files (an .xlsx) become file attachments; OLE containers show their preview picture. Text boxes → outline callouts (or paragraphs); groups, charts and SmartArt keep their pictures or fallbacks, with a warning. - Sections: the first section is
result.page(size, orientation, margins, gutter); headers/footers (default, first, even; link to previous; PAGE/NUMPAGES/SECTIONPAGES/DATE/TITLE/STYLEREF fields as live field atoms) and page numbering become plugin-header-footer settings; later sections are section breaks (continuous ones stay on the page) with their own page size, orientation, margins (gutter on the left) and columns (w:cols: count, spacing, separator line, unequal widths, each column laid out at its own width). Column breaks becomecolumnBreakBefore; odd/even page starts becomepageBreakBefore: 'odd' | 'even'(layout adds the blank page Word would). - Fields: TOC → plugin-toc's table of contents (rebuilt by Folio, titled by its "TOC Heading"), HYPERLINK and REF/PAGEREF
\h→ links, the rest keep their cached result. Bookmarks become blockids and internal links#id(declaredocxImportSchemain the editor so they survive). - Footnotes and endnotes → numbered superscript links and a "Notes" section with back-links (a pluggable
NoteMappingfor a future footnote plugin). Comments (with replies and done state) → plugin-comments threads andcommentmarks. Tracked changes are accepted (or rejected withtrackedChanges: 'reject') and reported, or kept as suggestions (trackedChanges: 'keep':suggestInsert/suggestDeletemarks). Equations (OMML) → LaTeX for plugin-math. Embedded fonts are deobfuscated intofont/ttfassets (result.fonts.embedded);availableFontsreports missing ones.
Safety: archive caps (maxBytes 400 MB uncompressed, maxEntries 20,000, maxBlocks 500,000, XML depth 256), declared sizes checked before inflating; external relationships are never fetched (only hyperlinks are kept, and only http(s)/mailto/tel/ftp); no DTD or entity expansion.
Performance: document.xml is decoded and parsed in 1 MB chunks and each body block is converted and dropped, so memory never holds the XML tree. A ~500-page book imports in ~0.1 s in Chrome; a 22 MB real document (66 MB of XML, 920 pictures) in ~1.8 s. Big files: importDocxInWorker.
- Font policy:
fonts: { policy: 'substitute' | 'warn' | 'strict', loader, available }is the same option every importer takes (see Fonts): embedded faces, stand-ins and missing families as per-familyfont-*warnings andfonts.report. - Fonts: with
resolveFonts(e.g. @nextgensoftwares/folio-fonts'createFontLoader), the document's embedded faces and its styles' families load before conversion (solineMetricsreads the faces that draw), the rest after; families the host has no face for get a metric-compatible stand-in (OFFICE_FALLBACKS) and are reported, one warning for missing families, one for stand-ins.w:strikeandw:dstrikeare separate toggles. - Line breaks: imports set
theme.lineBreaks: 'office'(officeLineBreaks): lines may also break before a digit that follows Arabic text, directly or after an opening bracket (المدة٥٦,القيمة(١٢)), as Word files break in LibreOffice; UAX #14 keeps those together. Documents made in Folio keep UAX #14. Measured with Arabic probe documents against LibreOffice: every differing line of that kind went away. (LibreOffice sometimes also breaks at a space before punctuation,word ,; that one isn't modelled yet.) - Math size: imports set
theme.math.textSize: true: Word draws equations (OMML, MathType) at the text's size. The host calibrates its math renderer once (MathMeasurer.textScale, KaTeX: 1.21) instead of keying on the file format; inserted equations carrytextSizethemselves. - Line positions: imports set
theme.leading: 'below'(wordLeading): the font's line gap above the text, extra "multiple" spacing below it, "at least" lines (lineHeightpx +lineRule: 'atLeast', a minimum a bigger run can't scale) with the text at the bottom, "exactly" lines with the baseline at 80%. Lines are sized by their runs' fonts only (the paragraph style's face is no floor), in whole twips as LibreOffice lays them out: a font's single line is its exact height (hhea ascent + descent- line gap) rounded to a twip; a "multiple" line is that times the multiple, in whole percent above single (halves rounding down: w:line 259 is 108%), truncated to a twip (Times New Roman 14pt at 1.5 = ⌊322 × 1.5⌋ = 483 twips = 32.2px); exact and at-least heights are whole twips. An empty paragraph's line is sized by its paragraph mark (
w:pPr/w:rPr), as Word and LibreOffice do. Heading styles map by the level paragraphs become (outline level first), so converters' "heading 3" at outline level 4 keeps its look; list paragraphs keep their right indent; list markers sit left-aligned at the hanging indent (listLevel.hanging).
- line gap) rounded to a twip; a "multiple" line is that times the multiple, in whole percent above single (halves rounding down: w:line 259 is 108%), truncated to a twip (Times New Roman 14pt at 1.5 = ⌊322 × 1.5⌋ = 483 twips = 32.2px); exact and at-least heights are whole twips. An empty paragraph's line is sized by its paragraph mark (
- Pictures in a line of text become inline
imageatoms (on the baseline, in the line, exported back as Word inline pictures); a picture alone in its paragraph stays a block whose distances are the paragraph's spacing plus the text's descent. Square-wrapped pictures keep their vertical offset (offsetY). Table rows keepw:trHeight(height,heightRule: 'exact'). - Headers and footers: each section's
w:pgMar/@w:headerand@w:footer(headerDistance/footerDistanceper section), parts inherited from the previous section when a section names none (never backwards: a cover section without parts shows none),titlePgper section; text boxes are imported once (DrawingML, not the VML fallback) and fields inside them (a page-anchored "Page N" box) get each page's value. A part with no content reserves no band.
Fidelity against LibreOffice (node tools/fidelity/import/run.mjs [file.docx] [--chunks N] [--fonts DIR]; --chunks compares each slice of N sections on its own, --fonts makes LibreOffice and Folio substitute Office fonts with a bundle; big files LibreOffice can't export directly go through ODT): prose, paragraph spacing, lists and tables match page counts with line positions within ~1 px. A 155-page converted e-book (149 sections): 155 pages in both, median word offset 0.9 px (was 158 pages, 95 px); with the playground's bundled stand-ins 151 vs LibreOffice's 152 (was 158). Known differences: a table break inside a row-spanning cell is avoided (Word and LibreOffice split there); fields in text boxes of the body keep their cached text; LibreOffice draws Arabic in Times New Roman with FreeSerif while hosts draw their own fallback.
HTML
.html, .htm, .xhtml, and Google Docs' "Download → Web page" zip (HTML plus images/). Parsed with htmlparser2 (MIT) into a light tree, no DOM, so it also runs in workers and Node.
- Headings, paragraphs, line breaks, block quotes, rules,
<pre>/<code>(language fromlanguage-xclasses → plugin-code), lists (nested, start, type,list-style-type; task checkboxes become ☐/☑ with a warning), definition lists, figures and captions. - Tables with
colspan/rowspan, header cells, background colours and pixel widths; captions become a centred paragraph. - Bold/italic/underline/strike/sub/sup/code/mark, links, and safe inline styles: colour, highlight, font family and size, alignment, direction, indents, line height, page breaks. Black text, white backgrounds and the font used by most of the text are dropped (they would pin the document to the source's look); the body font and size are reported in
result.theme. - Images:
data:URLs and zip entries become assets;http(s)images keep their URL (warning); local paths are dropped. Inline images move out of their paragraph (Folio images are blocks). - Math: KaTeX/MathJax output and MathML with a TeX annotation, and
data-latexelements, become equations (plugin-math). - Folio's own copies (
data-folioattributes) round-trip their layout attrs.
Google Docs
Three ways in, from best to quickest:
- File ▸ Download ▸ Microsoft Word (.docx) →
@nextgensoftwares/folio-docx(styles, comments, headers/footers, footnotes). - File ▸ Download ▸ OpenDocument (.odt) → the ODT importer, or Web page (.html, zipped) → the HTML importer (class styles, re-nested lists, unwrapped
google.com/url?q=links, images from the zip, Title/Subtitle as headings). - Copy and paste: the editor's HTML paste handles the clipboard flavour (the
<b id="docs-internal-guid-…">wrapper'sfont-weight:normalcancels its bold).
Markdown
CommonMark + GFM through markdown-it (MIT; linear-time, ~10 MB in under 2 s) with markdown-it-footnote and small built-in rules for math: headings, emphasis, strikethrough, inline code, links, images, <https://…> autolinks (bare URLs aren't linked), lists (task items as ☐/☑ with a warning), tables with column alignment, block quotes, rules, fenced code (language → plugin-code), math $…$ / $$…$$ (plugin-math), footnotes and inline ^[…] notes (as endnotes), YAML front matter (title, author, lang, date → meta). Raw HTML is never rendered: formatting tags are applied, everything else keeps only its text. Math is pandoc-style: $…$ must not start or end with a space and the closing $ must not be followed by a digit, so "costs $5 and $10" stays text.
LaTeX
@nextgensoftwares/folio-import-formats/latex (latexImporter) reads a .tex / .ltx file, or a .zip of the whole project (an Overleaf download). A built-in reader, not a TeX engine: it reads the document's structure and keeps the math as LaTeX.
- Inputs: in a zip, the main file is the
.texwith\documentclass(main.texfirst).\input,\include(a new page),\subfileand\importare read relative to it, with or without.tex, up to 16 levels.\includegraphicsfinds PNG, JPEG, GIF, WebP and BMP figures, with or without an extension and in the\graphicspathfolders. A.bblnext to the main file supplies the bibliography. For a single.tex, passresolveFile(path)in the import options to supply those files (bytes or null); without it they are reported as missing. - Structure:
\part…\subparagraph(and starred) → headings, with the highest level used as H1;\maketitle→ a centred title (a paragraph styled as a heading, outside the outline), authors (\and) and date;\title,\authorand babel/polyglossia's main language →meta; abstract, itemize/enumerate (nested; enumitem labels andstart)/description, quote, quotation, verse, center/flushleft/flushright, theorem-like environments (\newtheoremincluded) as "Theorem 2 (Name)." and proofs ending in ∎.\newpage/\clearpage→ page breaks; chapters start new pages. - Text:
\emph,\textbf,\textit,\texttt,\underline,\textsc(small caps),\sout,\textsuperscript,\textcolorand\colorbox(xcolor names,red!30mixes,\definecolor), font declarations ({\bfseries …}), accents (\'e,\"{o}), TeX ligatures (`` '' -- --- ~),\href,\url(safe links only). - Math:
$…$,\(…\),$$…$$,\[…\], equation, align, gather, multline, eqnarray, flalign, alignat (starred too) → editable math nodes (plugin-math).\newcommand,\renewcommand,\def,\letand\DeclareMathOperator(with arguments and an optional first argument) are expanded first, in text and math;\newenvironmenttoo. Numbered displays stay numbered environments (equation,align,gather;multlineas agatherwhose last line has the number,eqnarrayandflalignasalign), with\nonumberlines marked\notag: the document numbers them itself, in its own order, and renumbers them as it is edited (equation numbers).\labelnames the number its line has in the imported document (a footnote's equation is numbered where the footnote lands, as an endnote). Math in a caption, a heading or an optional argument is set inline and takes no number. In classes with chapters (book, report), where numbers read "2.1", each line keeps that number as an explicit\tag{2.1}instead. - References:
\ref,\eqref,\pageref,\cref/\Cref/\autorefresolve to section, equation, figure, table, theorem, item and footnote numbers (unknown labels show??, with a warning).\cite,\citep,\citet… →[1, 2, p. 5]from the bibliography (thebibliographyor the.bbl), else the keys. - Floats and tables: figure/table floats are centred with "Figure 2: …" captions; tabular, tabularx, longtable and array → tables with column alignment and
p{…}widths,\multicolumn,\multirow,\rowcolor/\cellcolor, and header rows (before booktabs'\midrule, or longtable's\endhead).\footnoteand\thanks→ endnotes. - Code: verbatim, Verbatim, lstlisting (
language=…or\lstset) and minted → code blocks with their language (plugin-code);\verband\lstinline→ inline code. - Arabic and other RTL languages: polyglossia's
\setmainlanguage{arabic}or babel's main language makes paragraphs right to left;\begin{english},\begin{otherlanguage}{…},\begin{Arabic},\selectlanguageswitch direction per block;\textenglish{…}and\textarabic{…}keep their text. - Everything else keeps its text: an unknown command keeps the text of its arguments and an unknown environment its content, listed in one
latex-unknownwarning. TikZ/PGF drawings, PDF/EPS/SVG figures and.bib-only bibliographies are reported. Nothing fails the import: unbalanced braces, unclosed environments and runaway macros (bounded expansion,latex-macro-loop) degrade to text.
Options: resolveFile?: (path) => Uint8Array | null | Promise<…>, latexNumbering?: false (leave section, caption and theorem numbers out of the text; references still resolve).
Plain text
Encoding detection: BOM (UTF-8, UTF-16 LE/BE), BOM-less UTF-16, strict UTF-8, then Windows-1252 (reported). Blank lines separate paragraphs; single line breaks stay as line breaks.
OpenDocument (.odt)
.odt, .ott and flat .fodt (LibreOffice, Google Docs, Collabora, OnlyOffice exports). The style cascade (defaults → parent chain → common → automatic styles) drives headings (outline level, Title/Subtitle and "Heading N" styles), paragraph alignment/indents/line height/keeps/page breaks, and character formatting. Also: lists (bullet/number per level, start, continue), tables (spans, covered cells, repeated rows/columns, column widths, backgrounds), images and text boxes (wrap → Folio placement: inline, square, top-and-bottom, behind, in front), the first page style (size, margins → result.page), headers/footers (default, first, even; page number/count, date, title fields) → plugin-header-footer, footnotes and endnotes → endnotes, comments with replies and ranges → plugin-comments, the table of contents → plugin-toc, meta.xml → meta. Charts, OLE objects and shapes are reported (shapes keep their text); tracked changes import as displayed.
RTF
A built-in tokenizer (groups, control words, \'hh with the document and per-font code pages, \u with \uc skipping, \bin). Paragraph and character formatting, font and colour tables, heading styles and outline levels, lists (Word list tables, WordPad \pn, nesting), tables (cell widths, horizontal and vertical merges, backgrounds), PNG/JPEG pictures, hyperlinks, footnotes (endnotes), headers/footers with PAGE/NUMPAGES fields, page size and margins, document info. WMF/EMF pictures, comments, text boxes and equations are reported.
EPUB
EPUB 2 and 3: container → OPF → spine; each chapter goes through the HTML importer and becomes an H1 section in reading order (titles from the nav or NCX when a chapter has no heading of its own; inner headings shift below it). The cover becomes the first image, a table of contents (plugin-toc) follows it, and every chapter starts on a new page. Metadata → meta. DRM-encrypted books are refused; font obfuscation is fine. Book CSS and fonts aren't imported.
PDF (reconstruction)
Best effort
A PDF has no paragraphs, headings or lists, only positioned text. The PDF importer rebuilds structure from the layout and always says so in a warning. It is never pixel-faithful: line breaks, spacing, fonts and page breaks follow your theme, not the PDF. There is no OCR.
pdf.js (Apache-2.0, an optional peer dependency, loaded on first use with scripting, XFA and network access off) extracts text with positions and fonts. Then: lines by baseline, reading order across columns, paragraphs (gaps, indents, font changes; joined across page and column breaks), de-hyphenation, headings by size relative to the body text (and short bold lines), bold/italic from font names, superscript footnote markers, bullet and numbered lists, links from annotations, images (decoded pixels re-encoded as PNG, placed in reading order), repeated headers/footers and page numbers removed, page size and estimated margins. Scanned PDFs (no text layer) get a clear warning and only their images. Tables come out as paragraphs. Pass pdfWorker: () => new Worker(...) to parse off the main thread.
Writing an importer
An importer is a plain object; defineImporter from @nextgensoftwares/folio-import-formats adds the context (limits, warnings, assets, abort, progress):
import { defineImporter } from '@nextgensoftwares/folio-import-formats';
export const csvImporter = defineImporter(
{ format: 'csv', extensions: ['csv'], mimeTypes: ['text/csv'], sniff: (s) => /\.csv$/i.test(s.name ?? '') },
async (src, ctx) => {
const rows = parse(new TextDecoder().decode(src.bytes));
await ctx.tick(0.5); // yields, honours abort, reports progress
if (rows.some((r) => r.length > 50)) ctx.warn('wide', 'Columns past 50 were dropped.');
return ctx.result('csv', [tableOf(rows)]);
},
{ maxBytes: 20 * 1024 * 1024 },
);