Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add start and end positions of tag nodes #246

Merged
merged 3 commits into from
Aug 1, 2024

Conversation

Alteras1
Copy link
Contributor

@Alteras1 Alteras1 commented Jul 29, 2024

Adds start and end position of the start/end of tag nodes. This gives developers more control in debugging issues. This also allows developers to perform manual string manipulation beyond the limitations of the parser.

'[bar]Foo Bar[/bar]'

will parse to:

[
  {
    tag: 'bar',
    attrs: {},
    content: ['Foo', ' ', 'Bar'],
    start: {
      from: 0,
      to: 5,
    },
    end: {
      from: 12,
      to: 18,
    },
  },
];
An example of string manipulation usage
const str = '[font=Source Sans 3]text[/font]'

parses to:

[
  {
    tag: 'bar',
    attrs: {
      3: "3",
      "Source": "Source"
      "Sans": "Sans"
    },
    content: [text'],
    start: {
      from: 0,
      to: 19,
    },
    end: {
      from: 23,
      to: 29,
    },
  },
];

Since quotes was not used and there is a single integer by itself, recovering the original full single value would be impossible without using string manipulation.

const isSingleValue = (attrs) => {
  const keys = Object.keys(attrs).join(" ");
  const vals = Object.values(attrs).join(" ");
  return keys === vals;
}

font: (node) => {
  let value = '';
  if (Object.values(node.attrs).length > 1 && isSingleValue(node.attrs)) {
    value = str.substring(node.start.from + 5, node.start.to - 1).trim();
    // value = "Source Sans 3"
  }

  ...
}

Improves accuracy of row/col error reporting. Now targets the start of the relevant token instead of the end.

Closes #134


Edit: Update description to reflect simpler language

Copy link

changeset-bot bot commented Jul 29, 2024

🦋 Changeset detected

Latest commit: 3af485a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@bbob/plugin-helper Minor
@bbob/parser Minor
@bbob/types Minor
@bbob/cli Minor
@bbob/core Minor
@bbob/html Minor
@bbob/preset Minor
@bbob/preset-html5 Minor
@bbob/preset-react Minor
@bbob/preset-vue Minor
@bbob/react Minor
@bbob/vue2 Minor
@bbob/vue3 Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Improves accuracy of row/col error reporting. Now targets the start of the relevant token instead of the end.
@JiLiZART
Copy link
Owner

Good work! I will check it later 👍

@JiLiZART
Copy link
Owner

All looks good for me! 👍

improve readability of end pos offset for no attr tags
@JiLiZART JiLiZART merged commit 4084874 into JiLiZART:master Aug 1, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add position in the string for each node
2 participants