Socket
Book a DemoInstallSign in
Socket

txt-ast-traverse

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

txt-ast-traverse

TxtNode traverse library

1.2.0
Source
npmnpm
Version published
Maintainers
1
Created
Source

txt-ast-traverse Build Status

txt-ast-traverse provide traversal functions for Markdown AST.

This traverse function is a fork of estraverse for azu/markdown-to-ast.

This library is a part of azu/textlint.

Installation

npm install txt-ast-traverse

Usage

var parse = require("markdown-to-ast").parse,
    Syntax = require("markdown-to-ast").Syntax;
var traverse = require("txt-ast-traverse").traverse,
    VisitorOption = require("txt-ast-traverse").VisitorOption;
var AST = parse("# Header\nHello*world*");
traverse(AST, {
    enter(node) {
        console.log("enter", node.type);
        if (node.type === Syntax.Strong) {
            return VisitorOption.Skip;
        }
    },
    leave(node) {
        console.log("leave", node.type);
    }
});

Traversal rule is the same with Estraverse.

Example

Markdown:

Hello *world*

AST:

{
    "start_line": 1,
    "start_column": 1,
    "end_line": 0,
    "children": [
        {
            "start_line": 1,
            "start_column": 1,
            "end_line": 0,
            "inline_content": [
                {
                    "c": "Hello",
                    "raw": "Hello",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 0
                        },
                        "end": {
                            "line": 1,
                            "column": 5
                        }
                    },
                    "range": [
                        0,
                        5
                    ],
                    "type": "Str"
                },
                {
                    "c": " ",
                    "raw": " ",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 5
                        },
                        "end": {
                            "line": 1,
                            "column": 6
                        }
                    },
                    "range": [
                        5,
                        6
                    ],
                    "type": "Str"
                },
                {
                    "c": [
                        {
                            "c": "world",
                            "raw": "world",
                            "loc": {
                                "start": {
                                    "line": 1,
                                    "column": 0
                                },
                                "end": {
                                    "line": 1,
                                    "column": 5
                                }
                            },
                            "range": [
                                0,
                                5
                            ],
                            "type": "Str"
                        }
                    ],
                    "raw": "*world*",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 6
                        },
                        "end": {
                            "line": 1,
                            "column": 13
                        }
                    },
                    "range": [
                        6,
                        13
                    ],
                    "type": "Emphasis"
                }
            ],
            "children": [],
            "raw": "Hello *world*",
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 13
                }
            },
            "range": [
                0,
                13
            ],
            "type": "Paragraph"
        }
    ],
    "raw": "Hello *world*",
    "loc": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 1,
            "column": 13
        }
    },
    "range": [
        0,
        13
    ],
    "type": "Document"
}

Traversal all from Root(Document node):

[enter, Syntax.Document],
// # Header
[enter, Syntax.Header],
[enter, Syntax.Str],
[leave, Syntax.Str],
[leave, Syntax.Header],
// => Paragraph
[enter, Syntax.Paragraph],
[enter, Syntax.Str],
[leave, Syntax.Str],
// *world*
[enter, Syntax.Emphasis],
[enter, Syntax.Str],
[leave, Syntax.Str],
[leave, Syntax.Emphasis],
// <= Paragraph
[leave, Syntax.Paragraph],
// End
[leave, Syntax.Document]

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

License

MIT

and

Includes Estraverse

Copyright (C) 2012-2013 Yusuke Suzuki
https://github.com/estools/estraverse/blob/master/LICENSE.BSD

Keywords

AST

FAQs

Package last updated on 04 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.