
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
@textlint/ast-traverse
Advanced tools
@textlint/ast-traverse provide traversal functions for TxtAST.
This traverse function is a fork of estraverse for @textlint/markdown-to-ast.
This library is a part of textlint/textlint.
npm install @textlint/ast-traverse
var parse = require("@textlint/markdown-to-ast").parse,
Syntax = require("@textlint/markdown-to-ast").Syntax;
var traverse = require("@textlint/ast-traverse").traverse,
VisitorOption = require("@textlint/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.
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]
You want to set property on Node.
Bad example:
var TraverseController = require("@textlint/ast-traverse").Controller;
var controller = new TraverseController();
controller.traverse(ast, {
enter: function (node, parent) {
node.parent = parent;// it cause a circular reference!
// do something
something(node);
}
});
node.parent = parent;
cause a circular reference!
Correct example:
var TraverseController = require("@textlint/ast-traverse").Controller;
var controller = new TraverseController();
controller.traverse(ast, {
enter: function (node, parent) {
// set property as non-enumerable value
Object.defineProperty(node, "parent", {
value: parent
});
// do something
something(node);
}
});
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT
and
Includes Estraverse
Copyright (C) 2012-2013 Yusuke Suzuki
https://github.com/estools/estraverse/blob/master/LICENSE.BSD
FAQs
TxtNode traverse library
The npm package @textlint/ast-traverse receives a total of 75,325 weekly downloads. As such, @textlint/ast-traverse popularity was classified as popular.
We found that @textlint/ast-traverse demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.