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

Support for sub- & superscript, without using html tags #2709

Closed
PeterHagen opened this issue Jan 15, 2023 · 3 comments
Closed

Support for sub- & superscript, without using html tags #2709

PeterHagen opened this issue Jan 15, 2023 · 3 comments

Comments

@PeterHagen
Copy link

Describe the feature
Currently marked.js doesn't seem to have support for sub- & superscript, without using html tags. This would be possible by using E=MC^2^ and log 2~a~ as examples

Why is this feature necessary?
It would compliment the overal usability of marked.js, which would make reading and writing the markdown much cleaner instead of using html in the text.

Describe alternatives you've considered
As described on subscript-in-markdown, and I'm not sure if this is documented anywhere else, using E=MC^2^ would result in superscript, while log 2~a~ would result in subscript. Unfortunately, GitHub also doesn't support it.

@UziTech
Copy link
Member

UziTech commented Jan 15, 2023

Also ~this is strike through~

this is strike through

So which one would override the other for GitHub?

@PeterHagen
Copy link
Author

The subscript method should not allow spaces in the text and I guess it’s only valid when written against a character or word. Something like CO~2~. In some markdown parsers, strike through requires 2 ~ I noticed.

@UziTech
Copy link
Member

UziTech commented Jan 15, 2023

You could create an extension for these something like:

// superscript.js
export default {
  name: 'superscript',
  level: 'inline',
  start(src) { return src.indexOf('^'); },
  tokenizer(src) {
    const match = src.match(/^\^(\w+)\^/);

    if (!match) {
      return;
    }

    return {
      type: 'superscript',
      raw: match[0],
      text: match[1]
    };
  },
  renderer(token) {
    return `<sup>${token.text}</sup>`;
  }
};
// subscript.js
export default {
  name: 'subscript',
  level: 'inline',
  start(src) { return src.indexOf('~'); },
  tokenizer(src) {
    const match = src.match(/^~(\w+)~/);

    if (!match) {
      return;
    }

    return {
      type: 'subscript',
      raw: match[0],
      text: match[1]
    };
  },
  renderer(token) {
    return `<sub>${token.text}</sub>`;
  }
};
import superscript from './superscript.js';
import subscript from './subscript.js';
import { marked } from 'marked';

marked.use({
  extensions: [
    superscript,
    subscript
  ]
};

console.log(marked.parse('test^superscript^ test~subscript~'));
// <p>test<sup>superscript</sup> test<sub>subscript</sub></p>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants