File size: 1,554 Bytes
01e655b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import mistune


class PlainTextRenderer(mistune.HTMLRenderer):
    def text(self, text):
        return text

    def link(self, text, url, title=None):
        return text

    def image(self, alt, url, title=None):
        return alt

    def emphasis(self, text):
        return text

    def strong(self, text):
        return text

    def block_code(self, code, info=None):
        # remove code
        return ""

    def block_quote(self, text):
        return text

    def heading(self, text, level):
        return text + "\n"

    def newline(self):
        return "\n"

    def list(self, text: str, ordered: bool, **attrs) -> str:
        if ordered:
            html = ""
            return html + "\n" + text + "\n"
        return "\n" + text + "\n"

    def list_item(self, text):
        return "" + text + "\n"

    def paragraph(self, text):
        return text + "\n"

    def codespan(self, text: str) -> str:
        # remove code
        return ""


def markdown_to_text(markdown_text):
    renderer = PlainTextRenderer()
    markdown = mistune.create_markdown(renderer=renderer)
    text = markdown(markdown_text)
    text = text.strip()
    return text


if __name__ == "__main__":
    markdown_text = """
# 标题

这是一个示例文本,其中包含 **加粗**、*斜体*、[链接](http://example.com) 和其他 Markdown 语法。

```ts
console.log(1)
```

- 列表项 1
- 列表项 2
- 列表项 3

> 这是一个引用。

`代码片段`
    """
    plain_text = markdown_to_text(markdown_text)
    print(plain_text)