Skip to content

Issues with providing highlighted lines #20

Description

@Fox32

I have trouble to use the highlighted lines and showLineNumbers feature.

```js {1,3-4} showLineNumbers
function fancyAlert(arg) {
  if (arg) {
    $.facebox({ div: '#foo' })
  }
}
```

In general the line numbers work, if I force showLineNumbers on plugin creation. I'm using next-mdx-remote.

I played around a little bit and build myself a small plugin that prints the HAST before rehype-prism-plus is executed. I'm not a rehype or remark expert at all! This is what my HAST looks like:

{
  type: 'element',
  tagName: 'code',
  properties: {
    className: [ 'language-js' ],
    metastring: '{1,3-4} showLineNumbers',
    '{1,3-4}': true,
    showLineNumbers: true
  },
  children: [
    {
      type: 'text',
      value: 'function fancyAlert(arg) {\n' +
        '  if (arg) {\n' +
        "    $.facebox({ div: '#foo' })\n" +
        '  }\n' +
        '}\n'
    }
  ],
  position: {
    start: { line: 6, column: 1, offset: 40 },
    end: { line: 12, column: 4, offset: 150 }
  }
}

The issue seems to be, that a property called data is expected that should contain the meta, but instead I have a metastring property as part of properties.

I have no idea if that problem is on my side, or incompatible packages, or if we should just fix it here (by supporting both).
For now I solve this using a plugin on my side, that I run before this plugin:

const fixMetaPlugin = (options = {}) => {
  return (tree) => {
    visit(tree, 'element', visitor);
  };

  function visitor(node, index, parent) {
    if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
      return;
    }

    node.data = { ...node.data, meta: node.properties.metastring };
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions