แปล Markdown เป็น Document Object Model (DOM)

หากต้องการอ่าน จัดการ และแก้ไขเนื้อหาและการจัดรูปแบบของเอกสารโดยทางโปรแกรม คุณต้องแปลเป็น Aspose.Words Document Object Model (DOM)

ตรงกันข้ามกับเอกสาร Word Markdown ไม่สอดคล้องกับ DOM ที่อธิบายไว้ในบทความ Aspose.Words Document Object Model (DOM) อย่างไรก็ตาม Aspose.Words มีกลไกของตัวเองในการแปลเอกสาร Markdown เป็น DOM และย้อนหลัง เพื่อให้เราสามารถทำงานกับองค์ประกอบต่างๆ เช่น การจัดรูปแบบข้อความ ตาราง ส่วนหัว และอื่นๆ ได้สำเร็จ

บทความนี้จะอธิบายวิธีการแปลฟีเจอร์ต่างๆ ของ markdown เป็น Aspose.Words DOM และแปลงกลับเป็นรูปแบบ Markdown ได้อย่างไร

ความซับซ้อนของการแปล Markdown – DOM – Markdown

ปัญหาหลักของกลไกนี้ไม่ใช่แค่การแปล Markdown เป็น DOM เท่านั้น แต่ยังต้องทำการแปลงแบบย้อนกลับด้วย เพื่อบันทึกเอกสารกลับเป็นรูปแบบ Markdown โดยสูญเสียน้อยที่สุด มีองค์ประกอบหลายอย่าง เช่น เครื่องหมายคำพูดหลายระดับ ซึ่งการแปลงแบบย้อนกลับไม่ใช่เรื่องเล็กน้อย

เครื่องมือแปลของเราช่วยให้ผู้ใช้ไม่เพียงทำงานกับองค์ประกอบที่ซับซ้อนในเอกสาร Markdown ที่มีอยู่ แต่ยังสามารถสร้างเอกสารของตนเองในรูปแบบ Markdown ด้วยโครงสร้างดั้งเดิมตั้งแต่ต้น หากต้องการสร้างองค์ประกอบต่างๆ คุณต้องใช้สไตล์ที่มีชื่อเฉพาะตามกฎบางอย่างที่อธิบายไว้ในบทความนี้ สไตล์ดังกล่าวสามารถสร้างโดยทางโปรแกรมได้

หลักการแปลทั่วไป

เราใช้การจัดรูปแบบ Font สำหรับบล็อกแบบอินไลน์ เมื่อไม่มีการโต้ตอบโดยตรงสำหรับคุณลักษณะ Markdown ใน Aspose.Words DOM เราจะใช้ลักษณะอักขระที่มีชื่อที่ขึ้นต้นด้วยคำพิเศษบางคำ

สำหรับบล็อกคอนเทนเนอร์ เราใช้การสืบทอดรูปแบบเพื่อแสดงคุณลักษณะ Markdown ที่ซ้อนกัน ในกรณีนี้ แม้ว่าจะไม่มีคุณลักษณะที่ซ้อนกัน แต่เราก็ยังใช้ลักษณะย่อหน้าด้วยชื่อที่ขึ้นต้นด้วยคำพิเศษบางคำ

รายการสัญลักษณ์แสดงหัวข้อย่อยและลำดับคือคอนเทนเนอร์บล็อกใน Markdown เช่นกัน การซ้อนของพวกมันจะแสดงในรูปแบบ DOM เช่นเดียวกับบล็อกคอนเทนเนอร์อื่นๆ ทั้งหมดที่ใช้การสืบทอดสไตล์ อย่างไรก็ตาม นอกจากนี้ รายการใน DOM มีการจัดรูปแบบตัวเลขที่สอดคล้องกันในรูปแบบรายการหรือการจัดรูปแบบย่อหน้า

บล็อกแบบอินไลน์

เราใช้การจัดรูปแบบ Font เมื่อแปลคุณลักษณะ markdown แบบอินไลน์ Bold, Italic หรือ Strikethrough

คุณสมบัติ Markdown Aspose.Words
Bold
{1}
Font.Bold = true
Italic
*italic text*
Font.Italic = true
Strikethrough
~Strikethrough text~
Font.StrikeThrough = true
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Make the text Strikethrough.
builder.Font.Strikethrough = true;
builder.Writeln("This text will be Strikethrough");

เราใช้ลักษณะอักขระที่มีชื่อที่ขึ้นต้นด้วยคำว่า InlineCode ตามด้วย dot (.) ที่เป็นตัวเลือก และ backticks (`) จำนวนหนึ่งสำหรับฟีเจอร์ InlineCode หากพลาด backtick ไปจำนวนหนึ่ง ระบบจะใช้ backtick หนึ่งอันเป็นค่าเริ่มต้น

คุณสมบัติ Markdown Aspose.Words
InlineCode
{1}
Font.StyleName = “InlineCode[.][N]”
Autolink
<scheme://domain.com>
<email@domain.com>
คลาส FieldHyperlink
Link
{1}
{2}
{3}
{4})
คลาส FieldHyperlink
Image
{1}
{2}
{3}
{4})
คลาส Shape

บล็อกคอนเทนเนอร์

เอกสารคือลำดับของบล็อกคอนเทนเนอร์ เช่น ส่วนหัว ย่อหน้า รายการ เครื่องหมายคำพูด และอื่นๆ บล็อกคอนเทนเนอร์แบ่งได้เป็น 2 ประเภท: บล็อกใบและคอนเทนเนอร์แบบซับซ้อน บล็อกใบไม้มีได้เฉพาะเนื้อหาแบบอินไลน์เท่านั้น ในทางกลับกัน คอนเทนเนอร์ที่ซับซ้อนก็สามารถมีบล็อกคอนเทนเนอร์อื่นๆ ได้ รวมถึงบล็อกใบไม้ด้วย

บล็อกใบ

ตารางด้านล่างแสดงตัวอย่างการใช้บล็อก Markdown Leaf ใน Aspose.Words:

คุณสมบัติ Markdown Aspose.Words
HorizontalRule
-----
นี่คือย่อหน้าธรรมดาที่มีรูปร่างตามแนวนอน:
DocumentBuilder.InsertHorizontalRule()
ATX Heading
# H1, ## H2, ### H3…
ParagraphFormat.StyleName = “Heading N” โดยที่ (1<= N <= 9)
ข้อมูลนี้ได้รับการแปลเป็นรูปแบบบิวท์อินและควรเป็นรูปแบบที่ระบุทุกประการ (ไม่อนุญาตให้ใช้ส่วนต่อท้ายหรือคำนำหน้า)
มิฉะนั้นจะเป็นเพียงย่อหน้าธรรมดาที่มีสไตล์สอดคล้องกัน
Setext Heading
=== (ถ้าระดับหัวเรื่อง 1)
--- (หากส่วนหัวระดับ 2)
ParagraphFormat.StyleName = “SetextHeading[some suffix]” ตามรูปแบบ “Heading N”
ถ้า (N >= 2) จะใช้ “Heading 2” มิฉะนั้นจะใช้ “Heading 1”
อนุญาตให้ใช้ส่วนต่อท้ายใดก็ได้ แต่ผู้นำเข้า Aspose.Words ใช้ตัวเลข “1” และ “2” ตามลำดับ
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("This is an H1 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading1 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading1");
builder.ParagraphFormat.Style = setexHeading1;
builder.Document.Styles["SetextHeading1"].BaseStyleName = "Heading 1";
builder.Writeln("Setext Heading level 1");

builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("This is an H3 tag");

// Reset styles from the previous paragraph to not combine styles between paragraphs.
builder.Font.Bold = false;
builder.Font.Italic = false;

Style setexHeading2 = builder.Document.Styles.Add(StyleType.Paragraph, "SetextHeading2");
builder.ParagraphFormat.Style = setexHeading2;
builder.Document.Styles["SetextHeading2"].BaseStyleName = "Heading 3";

// Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.
builder.Writeln("Setext Heading level 2");
Indented Code ParagraphFormat.StyleName = “IndentedCode[some suffix]”
Fenced Code
``` c#
if ()
then
else
```
ParagraphFormat.StyleName = “FencedCode[.][info string]”
[.] และ [info string] เป็นทางเลือก

คอนเทนเนอร์ที่ซับซ้อน

ตารางด้านล่างแสดงตัวอย่างการใช้ Markdown Complex Containers ใน Aspose.Words:

คุณสมบัติ Markdown Aspose.Words
Quote
> quote,
>> nested quote
ParagraphFormat.StyleName = “Quote[some suffix]”
ส่วนต่อท้ายในชื่อลักษณะเป็นทางเลือก แต่ตัวนำเข้า Aspose.Words ใช้หมายเลขลำดับ 1, 2, 3, …. ในกรณีที่มีเครื่องหมายคำพูดซ้อนกัน
การซ้อนถูกกำหนดผ่านสไตล์ที่สืบทอดมา
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// By default a document stores blockquote style for the first level.
builder.ParagraphFormat.StyleName = "Quote";
builder.Writeln("Blockquote");

// Create styles for nested levels through style inheritance.
Style quoteLevel2 = builder.Document.Styles.Add(StyleType.Paragraph, "Quote1");
builder.ParagraphFormat.Style = quoteLevel2;
builder.Document.Styles["Quote1"].BaseStyleName = "Quote";
builder.Writeln("1. Nested blockquote");
BulletedList
- Item 1
- Item 2
- Item 2a
- Item 2b
รายการสัญลักษณ์แสดงหัวข้อย่อยจะแสดงโดยใช้หมายเลขย่อหน้า:
ListFormat.ApplyBulletDefault()
รายการสัญลักษณ์แสดงหัวข้อย่อยสามารถมีได้ 3 ประเภท ต่างกันเพียงรูปแบบการกำหนดหมายเลขของระดับแรกเท่านั้น เหล่านี้คือ: ‘-’, ‘+’ หรือ ‘*’ ตามลำดับ
OrderedList
1. Item 1
2. Item 2
1) Item 2a
2) Item 2b
รายการที่เรียงลำดับจะแสดงโดยใช้หมายเลขย่อหน้า:
ListFormat.ApplyNumberDefault()
เครื่องหมายรูปแบบตัวเลขมีได้ 2 แบบ: ‘." และ ‘)’. เครื่องหมายเริ่มต้นคือ ‘.’
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyBulletDefault();
builder.ListFormat.List.ListLevels[0].NumberFormat = $"{(char) 0}.";
builder.ListFormat.List.ListLevels[1].NumberFormat = $"{(char) 1}.";

builder.Writeln("Item 1");
builder.Writeln("Item 2");

builder.ListFormat.ListIndent();

builder.Writeln("Item 2a");
builder.Writeln("Item 2b");

ตาราง

Aspose.Words ยังอนุญาตให้แปลตารางเป็น DOM ดังที่แสดงด้านล่าง:

คุณสมบัติ Markdown Aspose.Words
Table
ก | b
- | -
c | ด
คลาส Table, Row และ Cell
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

// Add the first row.
builder.InsertCell();
builder.Writeln("a");
builder.InsertCell();
builder.Writeln("b");

// Add the second row.
builder.InsertCell();
builder.Writeln("c");
builder.InsertCell();
builder.Writeln("d");

ดูสิ่งนี้ด้วย