我正在尝试将 md 转换为 docx,但在转换单个反引号时遇到问题。它确实应用了 sourceText 样式来运行,但也应用了覆盖运行样式的 ParagraphTextBody 并且它没有显示在 word 或 libre office 中。这是我使用的代码:
@Test
public void mdToDocx() throws Exception {
DataHolder OPTIONS = new MutableDataSet()
.set(Parser.EXTENSIONS, Arrays.asList())
.setFrom(ParserEmulationProfile.GITHUB_DOC);
String input =
"# Backticks MD file\n" +
"\n" +
"`this is single line of backticks`\n" +
"\n" +
"Tis is text which contains `text under backticks`\n" +
"\n" +
"\n" +
"`this is backticks` this is regular text\n";
Parser parserDocx = Parser.builder(OPTIONS).build();
DocxRenderer docxRenderer = DocxRenderer.builder(OPTIONS).build();
Node document2 = parserDocx.parse(input);
WordprocessingMLPackage template = WordprocessingMLPackage.createPackage();
File file = new File("/path/to/file/doc.docx");
docxRenderer.render(document2, template);
System.out.println("***********************************");
System.out.println(XmlUtils.marshaltoString(template
.getMainDocumentPart().getJaxbElement(), true, true));
System.out.println("***********************************");
template.save(file);
}
这是输出
<w:body>
<w:bookmarkStart w:name="backticks-md-file" w:id="1"/>
<w:bookmarkEnd w:id="1"/>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>Backticks MD file</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ParagraphTextBody"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="SourceText"/>
</w:rPr>
<w:t>this is single line of backticks</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ParagraphTextBody"/>
</w:pPr>
<w:r>
<w:t xml:space="preserve">Tis is text which contains </w:t>
</w:r>
<w:r>
<w:rPr>
<w:rStyle w:val="SourceText"/>
</w:rPr>
<w:t>text under backticks</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="ParagraphTextBody"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="SourceText"/>
</w:rPr>
<w:t>this is backticks</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> this is regular text</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:pgSz w:w="11907" w:h="16839" w:code="9"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>
</w:sectPr>
</w:body>
我附上了生成的 docx 文件。 idemo.docx