importFromDocx method

Root importFromDocx(
  1. List<int> bytes
)

Implementation

Root importFromDocx(List<int> bytes) {
  final archive = ZipDecoder().decodeBytes(bytes);
  _archive = archive;

  // --- FIX Bug 1: parse relationships for hyperlink URL resolution ---
  final relsFile = archive.files
      .where((f) => f.name == 'word/_rels/document.xml.rels')
      .firstOrNull;
  if (relsFile != null) {
    _parseRelationships(utf8.decode(relsFile.content as Uint8List));
  }

  // --- FIX Bug 3: parse numbering.xml for list type detection ---
  final numberingFile = archive.files
      .where((f) => f.name == 'word/numbering.xml')
      .firstOrNull;
  if (numberingFile != null) {
    _parseNumbering(utf8.decode(numberingFile.content as Uint8List));
  }

  final documentXml = archive.files
      .where((f) => f.name == 'word/document.xml')
      .firstOrNull;
  if (documentXml == null) return Root(nodes: [Paragraph(text: '')]);

  final xmlString = utf8.decode(documentXml.content as Uint8List);
  final document = XmlDocument.parse(xmlString);
  final body = document.findAllElements('w:body').firstOrNull;
  if (body == null) return Root(nodes: [Paragraph(text: '')]);

  final nodes = _elementsToNodes(body.children);
  return Root(nodes: nodes.isEmpty ? [Paragraph(text: '')] : nodes);
}