JiLiZART / BBob

⚡️Blazing fast js bbcode parser, that transforms and parses bbcode to AST and transform it to HTML, React, Vue with plugin support in pure javascript, no dependencies
https://codepen.io/JiLiZART/full/vzMvpd
MIT License
167 stars 19 forks source link

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

Closed Alteras1 closed 4 months ago

Alteras1 commented 4 months ago

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 ```js const str = '[font=Source Sans 3]text[/font]' ``` parses to: ```js [ { 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. ```js 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

changeset-bot[bot] commented 4 months ago

🦋 Changeset detected

Latest commit: 3af485abd2aedac0f264933fdd5f0074dea5330e

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

JiLiZART commented 4 months ago

Good work! I will check it later 👍

JiLiZART commented 4 months ago

All looks good for me! 👍