Làm việc với các tính năng của Markdown

Chủ đề này thảo luận cách triển khai các tính năng Markdown bằng Aspose.Words. Markdown là một cách đơn giản để định dạng văn bản thuần túy có thể dễ dàng chuyển đổi sang HTML. Aspose.Words hỗ trợ các tính năng Markdown sau:

  • Tiêu đề
  • Trích dẫn khối
  • Quy tắc ngang
  • Nhấn mạnh đậm
  • Nhấn mạnh chữ nghiêng

Việc triển khai tính năng Markdown chủ yếu tuân theo đặc tả CommonMark trong Aspose.Words API và tất cả các tính năng được biểu diễn dưới dạng kiểu tương ứng hoặc định dạng trực tiếp. Có nghĩa là

  • Chữ đậm và chữ nghiêng được thể hiện dưới dạng Font.boldFont.Italic
  • Heading là những đoạn văn có kiểu Heading 1 - Heading 6
  • Trích dẫn là những đoạn văn có chữ “Trích dẫn” trong tên kiểu
  • HorizontalRule là đoạn văn có hình dạng HorizontalRule.

Tài liệu Markdown có điểm nhấn

Phần này hướng dẫn bạn cách tạo tài liệu markdown với các điểm nhấn như dưới đây:

Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.
You can write **bold** or *italic* text. 
You can also write ***BoldItalic*** text.

Đoạn mã sau có thể được sử dụng để tạo tài liệu markdown đã cho ở trên.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.writeln("Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.")
builder.write("You can write ")
builder.font.bold = True
builder.write("bold")
builder.font.bold = False
builder.write(" or ")
builder.font.italic = True
builder.write("italic")
builder.font.italic = False
builder.writeln(" text. ")
builder.write("You can also write ")
builder.font.bold = True
builder.font.italic = True
builder.write("BoldItalic")
builder.font.bold = False
builder.font.italic = False
builder.write("text.")
builder.document.save(docs_base.artifacts_dir + "WorkingWithMarkdown.emphases.md")

Tài liệu Markdown có tiêu đề

Phần này hướng dẫn bạn cách tạo tài liệu markdown với các tiêu đề như bên dưới:

The following produces headings:
# Heading1
## Heading2
### Heading3
#### Heading4
##### Heading5
###### Heading6
# **Bold Heading1**

Đoạn mã sau có thể được sử dụng để tạo tài liệu markdown đã cho ở trên.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# By default Heading styles in Word may have bold and italic formatting.
# If we do not want the text to be emphasized, set these properties explicitly to false.
builder.font.bold = False
builder.font.italic = False
builder.writeln("The following produces headings:")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 1")
builder.writeln("Heading1")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 2")
builder.writeln("Heading2")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 3")
builder.writeln("Heading3")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 4")
builder.writeln("Heading4")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 5")
builder.writeln("Heading5")
builder.paragraph_format.style = doc.styles.get_by_name("Heading 6")
builder.writeln("Heading6")
# Note that the emphases are also allowed inside Headings.
builder.font.bold = True
builder.paragraph_format.style = doc.styles.get_by_name("Heading 1")
builder.writeln("Bold Heading1")
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdown.headings.md")

Tài liệu Markdown có trích dẫn khối

Phần này hướng dẫn bạn cách tạo tài liệu markdown có dấu ngoặc kép như dưới đây:

We support blockquotes in Markdown:
>*Lorem*
>*ipsum*
>The quotes can be of any level and can be nested:
>>>Quote level 3
>>>
>>>>Nested quote level 4
>
>*Back to first level*
>### Headings are allowed inside Quotes
>

Đoạn mã sau có thể được sử dụng để tạo tài liệu markdown đã cho ở trên.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.writeln("We support blockquotes in Markdown:")
builder.paragraph_format.style = doc.styles.get_by_name("Quote")
builder.writeln("Lorem")
builder.writeln("ipsum")
builder.paragraph_format.style = doc.styles.get_by_name("Normal")
builder.writeln("The quotes can be of any level and can be nested:")
quoteLevel3 = doc.styles.add(aw.StyleType.PARAGRAPH, "Quote2")
builder.paragraph_format.style = quoteLevel3
builder.writeln("Quote level 3")
quoteLevel4 = doc.styles.add(aw.StyleType.PARAGRAPH, "Quote3")
builder.paragraph_format.style = quoteLevel4
builder.writeln("Nested quote level 4")
builder.paragraph_format.style = doc.styles.get_by_name("Quote")
builder.writeln()
builder.writeln("Back to first level")
quoteLevel1WithHeading = doc.styles.add(aw.StyleType.PARAGRAPH, "Quote Heading 3")
builder.paragraph_format.style = quoteLevel1WithHeading
builder.write("Headings are allowed inside Quotes")
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdown.block_quotes.md")

Tài liệu Markdown có quy tắc ngang

Phần này hướng dẫn bạn cách tạo tài liệu markdown với Quy tắc ngang như dưới đây:

We support Horizontal rules (Thematic breaks) in Markdown:
-----

Đoạn mã sau có thể được sử dụng để tạo tài liệu markdown đã cho ở trên.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
builder = aw.DocumentBuilder()
builder.writeln("We support Horizontal rules (Thematic breaks) in Markdown:")
builder.insert_horizontal_rule()
builder.document.save(docs_base.artifacts_dir + "WorkingWithMarkdown.horizontal_rule_example.md")

Đọc tài liệu Markdown

Đoạn mã sau đây hướng dẫn bạn cách đọc tài liệu markdown.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Quotes.md")
# Let's remove Heading formatting from a Quote in the very last paragraph.
paragraph = doc.first_section.body.last_paragraph
paragraph.paragraph_format.style = doc.styles.get_by_name("Quote")
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdown.read_markdown_document.md")

Chỉ định tùy chọn lưu Markdown

Aspose.Words API cung cấp lớp MarkdownSaveOptions để chỉ định các tùy chọn bổ sung trong khi lưu tài liệu sang định dạng Markdown.

Ví dụ mã sau đây trình bày cách chỉ định các tùy chọn lưu Markdown khác nhau.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.writeln("Some text!")
doc.save(docs_base.artifacts_dir + "BaseConversions.docx_to_markdown.md")

Cách căn chỉnh nội dung bên trong bảng khi xuất sang Markdown

Aspose.Words API cung cấp bảng liệt kê TableContentAlignment xác định hướng căn chỉnh để căn chỉnh nội dung trong bảng trong khi xuất sang tài liệu Markdown. Ví dụ mã sau đây minh họa cách căn chỉnh nội dung bên trong bảng.

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.insert_cell()
builder.paragraph_format.alignment = aw.ParagraphAlignment.RIGHT
builder.write("Cell1")
builder.insert_cell()
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
builder.write("Cell2")
# Makes all paragraphs inside the table to be aligned.
saveOptions = aw.saving.MarkdownSaveOptions()
saveOptions.table_content_alignment = aw.saving.TableContentAlignment.LEFT
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdownSaveOptions.left_table_content_alignment.md", saveOptions)
saveOptions.table_content_alignment = aw.saving.TableContentAlignment.RIGHT
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdownSaveOptions.right_table_content_alignment.md", saveOptions)
saveOptions.table_content_alignment = aw.saving.TableContentAlignment.CENTER
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdownSaveOptions.center_table_content_alignment.md", saveOptions)
# The alignment in this case will be taken from the first paragraph in corresponding table column.
saveOptions.table_content_alignment = aw.saving.TableContentAlignment.AUTO
doc.save(docs_base.artifacts_dir + "WorkingWithMarkdownSaveOptions.auto_table_content_alignment.md", saveOptions)