Skip to content

Fix flawed unist-util-visit usage - #1447

Merged
0x2b3bfa0 merged 3 commits into
masterfrom
0x2b3bfa0-patch-1
May 9, 2024
Merged

0x2b3bfa0 merged 3 commits into
masterfrom
0x2b3bfa0-patch-1

Conversation

@0x2b3bfa0

@0x2b3bfa0 0x2b3bfa0 commented May 8, 2024 •

Copy link
Copy Markdown
Member

It turns out that unist-util-visit takes a Visitor function, that may optionally return an Index to "specify the
sibling to traverse after [the current] node is traversed"
, but Array.prototype.push "returns the new length of the array", causing an erratic tree traversal.

Minimal example

const remark = require('remark');
const visit = require('unist-util-visit');

const report = `
# Heading 1
[![description](https://img.shields.io/badge/...)](https://studio.iterative.ai/team/...)
## Heading 2
![one](./one)
![two](./two)
![three](./three)
`;

function plugin(tree) {
  const nodes = [];

  // The visitor function returns the result of calling nodes.push()
  visit(tree, ['definition', 'image', 'link'], (node) => nodes.push(node));

  nodes.map(({url}) => console.log(url));
}

remark().use(() => plugin).process(report);
https://studio.iterative.ai/team/...
https://img.shields.io/badge/...
./one
./three
./three

@0x2b3bfa0 0x2b3bfa0 added the bug Something isn't working label May 8, 2024
@0x2b3bfa0
0x2b3bfa0 requested a review from shcheklein May 8, 2024 08:05
@0x2b3bfa0 0x2b3bfa0 self-assigned this May 8, 2024
@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@github-actions

github-actions Bot commented May 8, 2024

Copy link
Copy Markdown
Contributor

Test Comment

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot @0x2b3bfa0 !

@0x2b3bfa0
0x2b3bfa0 merged commit 1c49104 into master May 9, 2024
@0x2b3bfa0
0x2b3bfa0 deleted the 0x2b3bfa0-patch-1 branch May 9, 2024 22:05

This branch was previously deployed

1 inactive deployment
internal — 8143dd36 Deployed May 8, 2024 by 0x2b3bfa0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants