- Commit
- aebf7308ef00a8c2150200dc641d3eeee80a8303
- Parent
- 82e759174bc8fcd8fd070a8b379adcdaa048e7cd
- Author
- John MacFarlane <jgm@berkeley.edu>
- Date
Added examples of using walk.
My personal build of CMark ✏️
Added examples of using walk.
1 file changed, 13 insertions, 1 deletion
Status | File Name | N° Changes | Insertions | Deletions |
Modified | commonmark.rb | 14 | 13 | 1 |
diff --git a/commonmark.rb b/commonmark.rb @@ -335,10 +335,22 @@ class HtmlRenderer < Renderer end doc = Node.parse_file(ARGF) + +# Walk tree and print URLs for links doc.walk do |node| if node.type == :link printf("URL = %s\n", node.url) - printf("parent is %s\n", node.parent.type) + end +end + +# Walk tree and transform links to regular text +doc.walk do |node| + if node.type == :link + parent = node.parent + index = parent.children.index(node) + len = parent.children.length + parent.children.replace(parent.children.slice(0,index) + node.children + + parent.children.slice(index + 1, len)) end end