arkTS 如何解析MD格式?

news/2024/9/23 23:33:31

1. 尝试1

interface Interface_1 {heading: RegExp;listItem: RegExp;paragraph: RegExp;
}const markdownRules: Interface_1 = {heading: /^#\s+(.*)$/,listItem: /^\s*-\s+(.*)$/,paragraph: /^([^\n]+)$/,
}// 解析 Markdown 文本
function parseMarkdown(markdownText: string) {const lines: string[] = markdownText.split('\n');const jsonData: string[] = [];for (const line of lines) {if (markdownRules.heading.test(line)) {const res: RegExpMatchArray | null = line.match(markdownRules.heading)if (res) {const heading: string = res[1];jsonData.push(heading);}} else if (markdownRules.listItem.test(line)) {const res: RegExpMatchArray | null = line.match(markdownRules.listItem)if (res){const listItem:string= res[1];jsonData.push(listItem);}} else if (markdownRules.paragraph.test(line)) {const res: RegExpMatchArray | null = line.match(markdownRules.paragraph)if (res){const paragraph:string= res[1];jsonData.push(paragraph);}}}return jsonData;
}interface LiteralInterface_1 {heading?: string;title?: string
}let array: LiteralInterface_1[] = [];// 添加对象
array.push({ heading: 'abc' });
array.push({ title: 'jjj' });console.log('',JSON.stringify(array));
// 示例用法
const markdownText = `
# 标题
这是一段 Markdown 文本。- 列表项 1
- 列表项 2
`;const jsonData = parseMarkdown(markdownText);
console.log(JSON.stringify(jsonData, null, 2));

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/46698.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

MySQL8-中文参考-三十二-

MySQL8 中文参考(三十二)原文:docs.oracle.com/javase/tutorial/reallybigindex.html17.12.2 在线 DDL 性能和并发性译文:dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-performance.html在线 DDL 改进了 MySQL 操作的几个方面:访问表的应用程序更具响应性,因为在 …

CC1补充-LazyMap利用

CC1的LazyMap链 分析了上一条链子,其实在TransformMap类那里有个分叉点,就是还可以利用另一个类LazyMap进行transform方法的调用。 进入到LazyMap类中,发现get方法也调用了transform方法:可以看到在调用方法之前有个if的判断,跟进这个containKey函数:翻译一手:也就是传入…

Git-中文参考-七-

Git 中文参考(七)原文:Git Reference 协议:CC BY-NC-SA 4.0git-daemon原文: git-scm.com/docs/git-daemon名称 git-daemon - Git 存储库的一个非常简单的服务器 概要 git daemon [--verbose] [--syslog] [--export-all][--timeout=<n>] [--init-timeout=<n>] …

Git-中文参考-二-

Git 中文参考(二)原文:Git Reference 协议:CC BY-NC-SA 4.0git-help原文: git-scm.com/docs/git-help 贡献者:honglyua名称 git-help - 显示有关 Git 的帮助信息 概要 git help [-a|--all [--[no-]verbose]] [-g|--guide][-i|--info|-m|--man|-w|--web] [COMMAND|GUIDE]描…

Git-中文参考-八-

Git 中文参考(八)原文:Git Reference 协议:CC BY-NC-SA 4.0git-show-ref原文: git-scm.com/docs/git-show-ref名称 git-show-ref - 列出本地存储库中的引用 概要 git show-ref [-q|--quiet] [--verify] [--head] [-d|--dereference][-s|--hash[=<n>]] [--abbrev[=&l…

华为阅读携手多家头部出版机构,共创数字阅读新纪元

华为开发者大会(HDC 2024)上,华为阅读不仅公布了基于AI的多项技术升级,同时也对外宣布了内容生态建设的最新进展。大会上,华为阅读同作家出版社、中国少年儿童新闻出版总社、上海外文图书有限公司、青岛出版社、新蕾出版社等合作伙伴举办合作庆功会,这一重要举措标志着华…

设计模式-解释器模式

解释器模式(InterPreter Pattern) 解释器模式是指给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用 该表示 来解释语言中的句子,并按照规定的语法进行解析的模式,属于行为型模式。 比如编译器可以将源码编译为机器码,让CPU能进行识别并运行。解释器模…