feat: handle image rendor
This commit is contained in:
@@ -3,21 +3,45 @@
|
||||
*
|
||||
* Uses 'mathml2omml' library to convert MathML to Office Math Markup Language (OMML).
|
||||
* This is a pure JavaScript implementation and does not require external XSLT files.
|
||||
*
|
||||
* @see https://github.com/fiduswriter/mathml2omml
|
||||
*/
|
||||
|
||||
import { mml2omml } from 'mathml2omml';
|
||||
|
||||
/**
|
||||
* Cleans up OMML output from mathml2omml to fix known library issues.
|
||||
* These are workarounds for bugs in mathml2omml that produce invalid OMML.
|
||||
*
|
||||
* @param omml Raw OMML string from mathml2omml
|
||||
* @returns Cleaned OMML string compatible with Microsoft Word
|
||||
*/
|
||||
function cleanOmml(omml: string): string {
|
||||
return omml
|
||||
// Fix: m:sty m:val="undefined" is invalid (library bug with mathvariant="normal")
|
||||
.replace(/<m:sty m:val="undefined"\/>/g, '')
|
||||
// Remove empty control properties that cause parsing issues in some Word versions
|
||||
.replace(/<m:ctrlPr\/>/g, '')
|
||||
.replace(/<w:rPr\/>/g, '')
|
||||
// Remove inline namespace declarations (will use wrapper's namespaces)
|
||||
.replace(/ xmlns:m="[^"]*"/g, '')
|
||||
.replace(/ xmlns:w="[^"]*"/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts MathML string to OMML string using mathml2omml library.
|
||||
*
|
||||
* @param mathml The MathML content string
|
||||
* @returns Promise resolving to OMML string
|
||||
* @returns Promise resolving to cleaned OMML string
|
||||
* @see https://github.com/fiduswriter/mathml2omml
|
||||
*/
|
||||
export async function convertMathmlToOmml(mathml: string): Promise<string> {
|
||||
try {
|
||||
// The library is synchronous, but we keep the async signature for compatibility
|
||||
// and potential future changes (e.g. if we move this to a worker).
|
||||
// Convert using mml2omml function
|
||||
const omml = mml2omml(mathml);
|
||||
return omml;
|
||||
|
||||
// Apply fixes for known library issues
|
||||
return cleanOmml(omml);
|
||||
} catch (error) {
|
||||
console.error('MathML to OMML conversion failed:', error);
|
||||
return '';
|
||||
@@ -25,26 +49,21 @@ export async function convertMathmlToOmml(mathml: string): Promise<string> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps OMML in Word XML clipboard format if needed.
|
||||
* This helps Word recognize it when pasting as text.
|
||||
* Wraps OMML for clipboard/paste into Microsoft Word.
|
||||
* Uses Office Open XML namespaces (Office 2007+).
|
||||
*
|
||||
* @param omml Cleaned OMML string from convertMathmlToOmml
|
||||
* @returns XML string ready for clipboard
|
||||
*/
|
||||
export function wrapOmmlForClipboard(omml: string): string {
|
||||
// Replace 2006 namespaces with 2004/2003 namespaces for Word 2003 XML compatibility
|
||||
// This is necessary because the clipboard format uses the older Word XML structure
|
||||
const compatibleOmml = omml
|
||||
.replace(/http:\/\/schemas\.openxmlformats\.org\/officeDocument\/2006\/math/g, 'http://schemas.microsoft.com/office/2004/12/omml')
|
||||
.replace(/http:\/\/schemas\.openxmlformats\.org\/wordprocessingml\/2006\/main/g, 'http://schemas.microsoft.com/office/word/2003/wordml');
|
||||
// Extract inner content from <m:oMath> tags to avoid nesting
|
||||
const innerContent = omml
|
||||
.replace(/<m:oMath[^>]*>/g, '')
|
||||
.replace(/<\/m:oMath>/g, '');
|
||||
|
||||
// Simple XML declaration wrapper often helps
|
||||
return `<?xml version="1.0"?>
|
||||
<?mso-application progid="Word.Document"?>
|
||||
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml">
|
||||
<w:body>
|
||||
<w:p>
|
||||
<m:oMathPara>
|
||||
${compatibleOmml}
|
||||
</m:oMathPara>
|
||||
</w:p>
|
||||
</w:body>
|
||||
</w:wordDocument>`;
|
||||
// Return with proper Office Open XML structure
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
||||
${innerContent}
|
||||
</m:oMath>`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user