@enkidevs/curriculum-parser-markdown

10.2.3 • Public • Published

Enki Curriculum Parser

Parses Enki Curriculum markdown into an AST.

The text processing architecture of Enki Curriculum is based on unifiedjs, using its markdown interfaces via remarkjs.

The parser generates an AST (sample AST) from a markdown string.

The generated AST follows the MDAST spec with some custom Enki Curriculum data.

This spec contains examples from this insight from the Enki Curriculum.

Table of Contents

Insight

Insight follows the regular MDAST spec with some additional plugins.

For information on the data (not AST structure) contained in an Insight, checkout the Enki Curriculum Wiki.

Headline

Headline (UnistParent) is a substitute node for a MDASTHeading of depth 1.

interface Headline <: UnistParent {
  type: "headline";
  value: string;
}

For example, the following markdown:

# Coding is fun

Yields:

{
  "type": "headline",
  "children": [{
    "type": "text",
    "value": "Coding is fun"
  }]
}

Image

Image (UnistNode) inherits from MDASTImage.

If the image url contains an encoded SVG (See How to add images to Enki Curriculum), the parses attaches the flag svg: true.

interface Image <: Node {
  type: "image";
  title: string | null;
  alt: string | null;
  url: string;
  svg?: true | false;
}

For example, the following markdown:

![balanced](%3Csvg%20width%3D%22100%25%22%20height%3D%22auto%22%20viewBox%3D%220%200%20700%20300%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ctitle%3EArtboard%3C%2Ftitle%3E%3Cg%20fill%3D%22none%22%20fill-rule%3D%22evenodd%22%3E%3Cpath%20d%3D%22M350%2085c19.329966%200%2035-15.6700338%2035-35%200-6.7781994-1.926799-13.1063707-5.262556-18.4666731C373.560339%2021.6072003%20362.551767%2015%20350%2015c-19.329966%200-35%2015.6700338-35%2035s15.670034%2035%2035%2035zM210%20185c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C233.560339%20121.6072%20222.551767%20115%20210%20115c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035zM100%20286c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C123.560339%20222.6072%20112.551767%20216%20100%20216c-19.3299662%200-35%2015.670034-35%2035s15.6700338%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Cpath%20d%3D%22M490%20185c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C513.560339%20121.6072%20502.551767%20115%20490%20115c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22465.996094%22%20y%3D%22164%22%3E13%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22197.998047%22%20y%3D%22167%22%3E3%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M600%20286c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C623.560339%20222.6072%20612.551767%20216%20600%20216c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22575.996094%22%20y%3D%22267%22%3E15%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M380%20285c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C403.560339%20221.6072%20392.551767%20215%20380%20215c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22355.996094%22%20y%3D%22266%22%3E10%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%2287.9980469%22%20y%3D%22267%22%3E1%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22337.998047%22%20y%3D%2266%22%3E8%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M325.305402%2075L232%20121.270754M376%2075l89.005263%2049.575516M125%20226.675909L185.724368%20176M516%20175l60.148477%2050.16054M405%20224.911949L464.957392%20175%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)

Yields:

{
  "type": "image",
  "title": null,
  "url": "%3Csvg%20width%3D%22100%25%22%20height%3D%22auto%22%20viewBox%3D%220%200%20700%20400%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ctitle%3EArtboard%3C%2Ftitle%3E%3Cg%20fill%3D%22none%22%20fill-rule%3D%22evenodd%22%3E%3Cpath%20d%3D%22M350%2095c19.329966%200%2035-15.6700338%2035-35%200-6.7781994-1.926799-13.1063707-5.262556-18.4666731C373.560339%2031.6072003%20362.551767%2025%20350%2025c-19.329966%200-35%2015.6700338-35%2035s15.670034%2035%2035%2035zM210%20195c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C233.560339%20131.6072%20222.551767%20125%20210%20125c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035zM100%20296c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C123.560339%20232.6072%20112.551767%20226%20100%20226c-19.3299662%200-35%2015.670034-35%2035s15.6700338%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3Cpath%20d%3D%22M490%20195c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C513.560339%20131.6072%20502.551767%20125%20490%20125c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22465.996094%22%20y%3D%22174%22%3E10%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22197.998047%22%20y%3D%22177%22%3E3%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M600%20296c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C623.560339%20232.6072%20612.551767%20226%20600%20226c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22575.996094%22%20y%3D%22277%22%3E15%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M535%20375c19.329966%200%2035-15.670034%2035-35%200-6.778199-1.926799-13.106371-5.262556-18.466673C558.560339%20311.6072%20547.551767%20305%20535%20305c-19.329966%200-35%2015.670034-35%2035s15.670034%2035%2035%2035z%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20fill%3D%22%23FFF%22%2F%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22510.996094%22%20y%3D%22356%22%3E13%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%2287.9980469%22%20y%3D%22277%22%3E1%3C%2Ftspan%3E%3C%2Ftext%3E%3Ctext%20font-family%3D%22RobotoMono-Light%2C%20Roboto%20Mono%22%20font-size%3D%2240%22%20font-weight%3D%22300%22%20fill%3D%22currentColor%22%3E%3Ctspan%20x%3D%22337.998047%22%20y%3D%2276%22%3E8%3C%2Ftspan%3E%3C%2Ftext%3E%3Cpath%20d%3D%22M325.305402%2085L232%20131.270754M376%2085l89.005263%2049.575516M125%20236.675909L185.724368%20186M516%20185l60.148477%2050.16054M561%20315.334131L575.702837%20285%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E",
  "alt": "unbalanced",
  "svg": true
}

Section

Section (UnistParent) is a custom node specific to Enki Curriculum.

It is created when text is prefixed with a MDASTThematicBreak followed by MDASTHeading of depth 2.

If a section is one of the question sections, it is marked with the flag question: true.

interface Section <: UnistParent {
  type: "section";
  name: string | null;
  question?: true | false;
}

For example, the following markdown:

---
## Content

A binary tree is called *balanced* if every leaf node is not more than a certain distance away from the root than any other leaf.

Yields:

{
  "type": "section",
  "name": "Content",
  "children": [
    {
      "type": "paragraph",
      "children": [
        {
          "type": "text",
          "value": "A binary tree is called "
        },
        {
          "type": "emphasis",
          "children": [
            {
              "type": "text",
              "value": "balanced"
            }
          ]
        },
        {
          "type": "text",
          "value": " if every leaf node is not more than a certain distance away from the root than any other leaf."
        }
      ]
    }
  ]
}

YAML

YAML (Text] follows the same convention as MDASTYAML.

The YAML metadata for an Insight must be the first content at the top (based on frontmatter rules).

This parser will additionally format links to contain a "nature" field.

For example, the following YAML:

---
author: enki
levels:
  - beginner
  - basic
  - medium
  - advanced
type: normal
category: must-know
links:
    - >-
        [Why is it safer to keep the tree balanced?](http://stackoverflow.com/questions/8015630/definition-of-a-balanced-tree){website}
parent: removing-keys-from-a-binary-search-tree
---

Yields:

{
  "type": "yaml",
  "value": "author: enki\n\nlevels:\n\n  - beginner\n  - basic\n  - medium\n  - advanced\n\ntype: normal\n\ncategory: must-know\n\nlinks:\n    - >-\n        [Why is it safer to keep the tree balanced?](http://stackoverflow.com/questions/8015630/definition-of-a-balanced-tree){website}\n\nparent: removing-keys-from-a-binary-search-tree",
  "data": {
    "parsedValue": {
      "author": "enki",
      "levels": [
        "beginner",
        "basic",
        "medium",
        "advanced"
      ],
      "type": "normal",
      "category": "must-know",
      "links": [
        {
          "name": "Why is it safer to keep the tree balanced?",
          "url": "http://stackoverflow.com/questions/8015630/definition-of-a-balanced-tree",
          "nature": "website"
        }
      ],
      "parent": "removing-keys-from-a-binary-search-tree"
    }
}

Question

The question parser is a parser for the content of any question section (Section).

AnswersList

AnswersList (UnistParent) inherits from MDASTList.

The AnswersList list is the last MDASTList in a question section (when using the Insight parser) or last child of MDASTRoot (when using the Question parser). It is always unordered and is marked with the flag answers: true.

The children of AnswersList is an array of AnswersListItem, sorted top-down by correctness.

The number of correct answers equals the number of QuestionGaps in the AswerList's parent AST.

interface AnswersList <: Parent {
  type: "list";
  ordered: false;
  start: null;
  spread: true | false;
  answers: true;
}

For example, the following markdown:

* Ordered linked list
* Ordered array
* Weighted graph
* Max-heap

Yields:

{
  "type": "list",
  "ordered": false,
  "start": null,
  "spread": false,
  "answers": true,
  "children": [
    {
      "type": "listItem",
      "spread": false,
      "checked": null,
      "correct": true,
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "Ordered linked list"
            }
          ]
        }
      ],
    },
    {
      "type": "listItem",
      "spread": false,
      "checked": null,
      "correct": false,
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "Ordered array"
            }
          ]
        }
      ],
    },
    {
      "type": "listItem",
      "spread": false,
      "checked": null,
      "correct": false,
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "Weighted graph"
            }
          ]
        }
      ]
    },
    {
      "type": "listItem",
      "spread": false,
      "checked": null,
      "correct": false,
      "children": [
        {
          "type": "paragraph",
          "children": [
            {
              "type": "text",
              "value": "Max-heap"
            }
          ]
        }
      ]
    }
  ]
}

AnswersListItem

AnswersListItem (UnistParent) inherits from MDASTListItem.

It is a direct child of AnswersList and does not contain a checkbox.

AnswersListItem also has a boolean flag correct where true means that item is one of the correct answers for an AnswersList and false means it is not.

interface AnswersListItem <: Parent {
  type: "listItem";
  spread: true | false;
  checked: null;
  correct: true | false;
}

For an example, see the definition of AnswersList.

Code

TODO

QuestionGap

QuestionGap (UnistNode) is a custom node specific to Enki Curriculum.

It is created anytime the string "???" is encountered.

For example, the following markdown:

???

Yields:

{
  "type": "questionGap",
  "value": "???"
}

QuestionHeadline

QuestionHeadline (UnistParent) is a substitute node for a MDASTHeading of depth 3 within a question section (Section).

interface QuestionHeadline <: UnistParent {
  type: "questionHeadline";
}

For example, the following markdown:

### What is the output of the following script?

Yields:

{
  "type": "questionHeadline",
  "children": [
    {
      "type": "text",
      "value": "What is the output of the following script?"
    }
  ]
}

API

const {
  contentTypes
} = require('@enkidevs/curriculum-helpers')
const {
  getParser
} = require('@enkidevs/curriculum-parser-markdown')

const ast = getParser(contentTypes.INSIGHT).parseSync(markdownString)

Dependents (2)

Package Sidebar

Install

npm i @enkidevs/curriculum-parser-markdown

Weekly Downloads

12

Version

10.2.3

License

MIT

Unpacked Size

31.2 kB

Total Files

16

Last publish

Collaborators

  • kapnobatai137
  • spypsy
  • mathieudutour
  • lukem512
  • stefanoeb
  • loopiezlol
  • nem035
  • oliviaoddo
  • pedromdrp
  • lizthedeveloper
  • sourabhv
  • milesflo
  • mihaiberq
  • enkici