@active-mdx/core 中文文档教程

发布于 2 年前 浏览 17 更新于 2 年前

Active MDX

MDX 的内容建模

在线文档

MDX 是一个很棒的Markdown 的增强功能。 在常规 Markdown 呈现为静态 html 的地方,MDX 为您提供了一个动态的、基于 React 的输出,您可以自定义并使其具有交互性,无论是通过嵌入 React 组件,还是自定义用于呈现 Markdown 输出的组件。

ActiveMDX 在 Node.js 中运行,并在文件夹及其子文件夹中的 MDX 文件的 集合 之上提供结构化内容建模 API。 这使您可以像处理数据库一样处理文档集合,将单个文档视为记录,并且内容结构(和 YAML frontmatter)决定其属性。 当您将 MDX 和 React 的自定义表示与从内容本身派生的数据相结合时,它会开启强大的可能性。

Active MDX 是一个内容建模库,可让您为项目中不同类型的 MDX 文档开发模型模型 假定文档遵循已知的标题和子标题结构。 例如,Recipe 将有一个 ## Ingredients 部分和一个 ## Steps 部分。 活动 MDX 模型允许您通过提供帮助程序来处理 AST 形式中的文档,从而将任何 MDX 文档转换为 JSON 对象。 进入 JSON 对象的内容将完全取决于您在此可预测结构中编写的内容。 Active MDX 模型由 Document 类提供支持,该类为您提供许多实用程序,用于将正在编写的内容转换为结构化数据。

Active MDX 根据所写内容提供的数据可用于支持各种应用程序。 如果你正在建立一个网站,这是一本有十几种食谱的食谱,Active MDX 可以为你提供所有成分和数量的数据库,你可以呈现一个按钮来下订单,让用户根据什么成分找到食谱他们有。

Requirements

  • Node.js 14.15.0 or later.

Installation

$ npm install active-mdx

Usage

ActiveMDX 在服务器上运行,在 Node.js 中。 使用本机支持 esm 模块的更高版本的节点。

首先,您有一个包含 mdx 文件的文件夹。

├── epics
│   ├── authentication.mdx
│   └── search.mdx
├── index.js
├── index.mdx
├── models
│   ├── Epic.js
│   └── Story.js
└── stories
    ├── authentication
    │   └── a-user-should-be-able-to-register.mdx
    └── search
        └── searching-for-a-product-by-category.mdx

此文件夹由 Active MDX Collection 的实例表示,

// index.js
import { Collection } from "@active-mdx/core"

// load({ models: true }) will automatically register ./models/*.js as model classes
export default new Collection({ rootPath: "./content" })

您可以在任何地方使用它

import collection from './index.js'

collection.load({ models: true }).then(async (collection) => {
  const Epic = collection.model('Epic')
  const epics = Epic.query(qb => qb.where("status", "completed")).fetchAll()
})

epics 文件夹由我们在 中定义的 模型 表示。 /models/Epic.js。 模型定义了该文档如何与项目中的其他文档相关,以及如何将文档内容中的信息表示为数据。

// ./models/Epic.js
import { Model } from "@active-mdx/core"
import Story from "./Story.js"

export default class Epic extends Model {
  stories() {
    return this.hasMany(Story, {
      heading: "stories",
      meta: () => ({ epic: this.title.toLowerCase() })
    })
  }

  get isComplete() {
    return this.stories().fetchAll().every((story) => story.meta.status === 'completed')
  }

  static is(document) {
    return document.id.startsWith("epic")
  }
}

这将采用 mdx 内容,例如 epics/authentication.mdx

---
status: proposed
---

# Authentication

The Authentication stories cover users logging in and out of the application, as well as the roles and permissions granted to these users and how they are enforced in the application.

## Stories

### A User should be able to register

As a User I would like to register so that I can use the application.

### A User should be able to login

As a User I would like to login so that I can use the application.

除了以 MDX 形式显示 Epic,我们还可以使用它作为数据和参考写作本身所包含的事物。

const authEpic = collection.getModel("epics/authentication")
console.log(authEpic.toJSON({ related: ["stories"], attributes: ["isComplete"] }))
/*
{
  "id": "epics/authentication",
  "meta": {
    "status": "proposed"
  },
  "title": "Authentication",
  "stories": [
    {
      "id": "stories/authentication/a-user-should-be-able-to-register",
      "meta": {
        "epic": "authentication"
      },
      "title": "A User Should be able to Register"
    },
    {
      "id": "stories/authentication/a-user-should-be-able-to-login",
      "meta": {
        "epic": "authentication"
      },
      "title": "A User should be able to login"
    }, ...
  ],
  "isComplete": false
}
*/

每个模型都可以访问底层的 Document 类,该类为 提供方法查询 ASTAST 节点的快捷方式,可用于从书写内容中提取数据。

Document 类 API 还提供了以编程方式操作文档内容的方法,例如

  • replaceSectionContent("Section Title", "- new\n - markdown\n - list\n")
  • appendToSection("Section Title", "[Link](www.google.com)")

获取 API 中可用内容的完整列表 查看 API 文档

CLI

该包附带一个 bin amdx,可用于初始化新项目,并使用文档和模型。

$ amdx --help

Guides and Documentation

Example Projects

Active MDX

Content Modeling for MDX

Online Documentation

MDX is a great enhancement for Markdown. Where regular Markdown renders to static html, MDX provides you with a dynamic, react based output which you can customize and make interactive, whether by embedding React components, or customizing the components used to render the markdown output.

ActiveMDX works in Node.js and provides a structured content modeling API on top of Collections of MDX files in a folder and its subfolders. This lets you work with the collection of documents as if it was a database, an individual document being a record, and the structure of the content ( and YAML frontmatter ) determining its attributes. When you combine the custom presentation of MDX and React, with data derived from the content itself, it opens up powerful possibilities.

Active MDX is a Content Modeling library which lets you develop Models for the different types of MDX documents in your project. A Model assumes that a document follows a known heading and sub-heading structure. For example, A Recipe will have an ## Ingredients section and a ## Steps section. Active MDX Models allow you to turn any MDX Document into a JSON object by providing helpers for working with the document in AST form. What goes into the JSON object will depend entirely on what you are writing inside of this predicable structure. Active MDX Models are backed by a Document class which provides you with many utilities for turning the content being written about into structured data.

The data Active MDX makes available based on what is being written about can be used to power all sorts of applications. If you are building a website which which is a cookbook that has a dozen recipes, Active MDX can give you a database of all of the ingredients and quantities and you could render a button to place the order let users find a recipe based on what ingredients they had.

Requirements

  • Node.js 14.15.0 or later.

Installation

$ npm install active-mdx

Usage

ActiveMDX works on the server, in Node.js. Use later versions of node which support esm modules natively.

To start with, you have a folder that contains mdx files.

├── epics
│   ├── authentication.mdx
│   └── search.mdx
├── index.js
├── index.mdx
├── models
│   ├── Epic.js
│   └── Story.js
└── stories
    ├── authentication
    │   └── a-user-should-be-able-to-register.mdx
    └── search
        └── searching-for-a-product-by-category.mdx

This folder is represented by an instance of the Active MDX Collection

// index.js
import { Collection } from "@active-mdx/core"

// load({ models: true }) will automatically register ./models/*.js as model classes
export default new Collection({ rootPath: "./content" })

You can use this anywhere

import collection from './index.js'

collection.load({ models: true }).then(async (collection) => {
  const Epic = collection.model('Epic')
  const epics = Epic.query(qb => qb.where("status", "completed")).fetchAll()
})

The mdx files inside of the epics folder are represented by a Model that we defined in ./models/Epic.js. A Model defines how this document relates to other documents in the project, and how information from the content of the document can be represented as data.

// ./models/Epic.js
import { Model } from "@active-mdx/core"
import Story from "./Story.js"

export default class Epic extends Model {
  stories() {
    return this.hasMany(Story, {
      heading: "stories",
      meta: () => ({ epic: this.title.toLowerCase() })
    })
  }

  get isComplete() {
    return this.stories().fetchAll().every((story) => story.meta.status === 'completed')
  }

  static is(document) {
    return document.id.startsWith("epic")
  }
}

This will take mdx content such as epics/authentication.mdx

---
status: proposed
---

# Authentication

The Authentication stories cover users logging in and out of the application, as well as the roles and permissions granted to these users and how they are enforced in the application.

## Stories

### A User should be able to register

As a User I would like to register so that I can use the application.

### A User should be able to login

As a User I would like to login so that I can use the application.

In addition to displaying the Epic in MDX form, we can also work with it as data and reference the things contained in the writing itself.

const authEpic = collection.getModel("epics/authentication")
console.log(authEpic.toJSON({ related: ["stories"], attributes: ["isComplete"] }))
/*
{
  "id": "epics/authentication",
  "meta": {
    "status": "proposed"
  },
  "title": "Authentication",
  "stories": [
    {
      "id": "stories/authentication/a-user-should-be-able-to-register",
      "meta": {
        "epic": "authentication"
      },
      "title": "A User Should be able to Register"
    },
    {
      "id": "stories/authentication/a-user-should-be-able-to-login",
      "meta": {
        "epic": "authentication"
      },
      "title": "A User should be able to login"
    }, ...
  ],
  "isComplete": false
}
*/

Every model has access to an underlying Document class, which provides methods for Querying the AST and Shortcuts to AST Nodes which can be used to extract data from the writing content.

The Document class API also provides methods for manipulating the content of the documents programatically such as

  • replaceSectionContent("Section Title", "- new\n - markdown\n - list\n")
  • appendToSection("Section Title", "[Link](www.google.com)")

For a full list of what is available in the API See The API Documentation

CLI

The package ships with a bin amdx which can be used to initialize a new project, and work with the documents and models.

$ amdx --help

Guides and Documentation

Example Projects

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文