feat: unkown
This commit is contained in:
50
src/lib/ommlConverter.ts
Normal file
50
src/lib/ommlConverter.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* MathML to OMML Converter
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { mml2omml } from 'mathml2omml';
|
||||
|
||||
/**
|
||||
* Converts MathML string to OMML string using mathml2omml library.
|
||||
* @param mathml The MathML content string
|
||||
* @returns Promise resolving to OMML string
|
||||
*/
|
||||
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).
|
||||
const omml = mml2omml(mathml);
|
||||
return omml;
|
||||
} catch (error) {
|
||||
console.error('MathML to OMML conversion failed:', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps OMML in Word XML clipboard format if needed.
|
||||
* This helps Word recognize it when pasting as text.
|
||||
*/
|
||||
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');
|
||||
|
||||
// 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>`;
|
||||
}
|
||||
Reference in New Issue
Block a user