json2md:基于 OpenHarmony 的 JSON 转 Markdown 转换工具

用户可通过该项目将 JSON 数据转换为 Markdown 格式,使数据更易读、易分享。支持标题、列表、图片、代码块等多种元素转换,提供同步和异步转换方法,还可扩展自定义标签类型。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago

json2md

简介

json2md是一个JSON转Markdown转换器。旨在通过将JSON数据转换为Markdown格式,使数据更易读、更易分享。例如,将以下JSON数据转换为Markdown格式:

{ h1: "JSON To Markdown" }转换为
# JSON To Markdown
{ ul: [ "项目1", "项目2", "项目3" ] }转换为
- 项目1
- 项目2
- 项目3

下载安裝

ohpm install @ohos/json2md

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包。

使用说明

1.通过json2md方法将JSON数据同步转换为Markdown格式文本。

import {Json2MD} from '@ohos/json2md';
const json2md = Json2MD.json2md;
let result: string = json2md([
  { h1: "JSON To Markdown" },
  { blockquote: "A JSON to Markdown converter." },
  {
    img: [
      { title: "Some image", source: "https://example.com/some-image.png" },
      { title: "Another image", source: "https://example.com/some-image1.png" },
      { title: "Yet another image", source: "https://example.com/some-image2.png" }
    ]
  },
  { h2: "Features" },
  {
    ul: [
      "Easy to use",
      "You can programmatically generate Markdown content",
      "..."
    ]
  },
  { h2: "How to contribute" },
  {
    ol: [
      "Fork the project",
      "Create your branch",
      "Raise a pull request"
    ]
  },
  { h2: "Code blocks" },
  { p: "Below you can see a code block example." },
  {
    code: {
      language: "js",
      content: [
        "function sum (a, b) {",
        "   return a + b",
        "}",
        "sum(1, 2)"
      ]
    }
  }
]);
console.log(result);  

2.通过json2md.async方法将JSON数据异步转换为Markdown格式文本。

import {Json2MD} from '@ohos/json2md';
const json2md = Json2MD.json2md;
json2md.async([
  { h1: "JSON To Markdown" },
  { blockquote: "A JSON to Markdown converter." }
]).then((result: string) => {
  console.log(result);
}).catch((e: Error) => {
  console.log(e.message);
});

json2md(data, prefix)

将JSON输入转换为markdown。 支持的元素

Type Element Data Example
h1 Heading 1 The heading text as string. { h1: "heading 1" }
h2 Heading 2 The heading text as string. { h2: "heading 2" }
h3 Heading 3 The heading text as string. { h3: "heading 3" }
h4 Heading 4 The heading text as string. { h4: "heading 4" }
h5 Heading 5 The heading text as string. { h5: "heading 5" }
h6 Heading 6 The heading text as string. { h6: "heading 6" }
p Paragraphs The paragraph text as string or array (multiple paragraphs). { p: "Hello World"} or multiple paragraphs: { p: ["Hello", "World"] }
blockquote Blockquote The blockquote as string or array (multiple blockquotes) { blockquote: "Hello World"} or multiple blockquotes: { blockquote: ["Hello", "World"] }
img Image An object or an array of objects containing the title, source and alt fields. { img: { title: "My image title", source: "http://example.com/image.png", alt: "My image alt" } }
ul Unordered list An array of strings or lists representing the items. { ul: ["item 1", "item 2"] }
ol Ordered list An array of strings or lists representing the items. { ol: ["item 1", "item 2"] }
hr Separator None { hr: "" }
code Code block element An object containing the language (String) and content (Array or String) fields. { code: { "language": "html", "content": "<script src='dummy.js'></script>" } }
table Table An object containing the headers (Array of Strings) and rows (Array of Arrays or Objects). { table: { headers: ["a", "b"], rows: [{ a: "col1", b: "col2" }] } } or { table: { headers: ["a", "b"], rows: [["col1", "col2"]] } }
link Link An object containing the title and the source fields. { title: 'hello', source: 'https://ionicabizau.net' }

您可以扩展 json2md.converters 对象来支持您的自定义类型,例如:

json2md.converters['custom'] = (input: DataInput, json2md: InputFunc) => {
   return `Hello ${input}!`
}
json2md.converters.h2 = (input: DataInput, json2md: InputFunc) => {
   return `Custom h2: ${input}`
}

然后您可以使用它:

json2md([{ h2: "title" },{ "custom": "World" }])
// => "Custom h2: title"
// => "Hello World!"

接口说明

接口名 参数 功能描述
json2md data:DataInput,prefix?:string 将JSON格式数据同步转换为Markdown格式文本,可以为生成的Markdown每一行添加指定前缀
json2md.async data:DataInput,prefix?:string 将JSON格式数据异步转换为Markdown格式文本,可以为生成的Markdown每一行添加指定前缀
json2md.converters None 可扩展的转换器集合对象,支持修改已有标签类型转换回调函数或新增自定义标签类型转换回调函数

约束与限制

在下述版本验证通过:

  • DevEco Studio: DevEco Studio 6.0.1 Release(6.0.1.251), SDK: API12(5.0.0.71)

目录结构

/json2md              # 项目根目录
├── AppScope/        # 应用范围配置
│   └── resources/   # 应用资源文件
├── entry/           # 应用入口
│   └── src/         # 入口源代码
├── library/         # 库模块
│   └── src/         # 库源代码
│       └─ main/ets/lib/  # 核心库代码
├── hvigor/          # 构建工具配置
├── README_zh.md     # 中文文档
├── LICENSE          # 许可证文件
└── oh-package.json5 # OpenHarmony包配置

贡献代码

使用过程中发现任何问题都可以提交 Issue,当然,也非常欢迎提交 PR 。

开源协议

本项目遵循 BSD-2-Clause, 请自由的享受和参与开源。

Introduction

用户可通过该项目将 JSON 数据转换为 Markdown 格式,使数据更易读、易分享。支持标题、列表、图片、代码块等多种元素转换,提供同步和异步转换方法,还可扩展自定义标签类型。【此简介由AI生成】

Customize your domain