All files / src/rules/converters docx-converter.ts

43.33% Statements 13/30
100% Branches 1/1
50% Functions 1/2
43.33% Lines 13/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   1x  
import * as vscode from 'vscode';
import * as mammoth from 'mammoth';
import { RuleConverter } from './converter';
import { convertContentWithAI } from '../import-service';
import { buildSystemPrompt } from './prompt-builder';
import type { CustomRule } from '../../types';
import { t } from '../../i18n/messages';
 
export class DocxConverter implements RuleConverter {
  supportedExtensions = ['.docx'];
 
  async convert(srcPath: string, context: vscode.ExtensionContext, existingRules?: CustomRule[]): Promise<string | null> {
    let result: mammoth.Result;
    try {
      result = await mammoth.extractRawText({ path: srcPath });
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      vscode.window.showErrorMessage(t('import.docxReadFail', { 0: msg }));
      return null;
    }

    const content = result.value.trim();
    if (!content) {
      vscode.window.showErrorMessage(t('import.docxEmpty'));
      return null;
    }

    return convertContentWithAI(content, context, buildSystemPrompt('freeform', existingRules));
  }
}