question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Options object for custom types

See original GitHub issue

Currently, it’s impossible to apply attributes to types that are not hardcoded. I tried to implement my self this feature but i think it needs a bit of re-do and i don’t fully understand what this plugin does internally 😦

My idea is, pass an option object, like this:

md.use(MarkdownItAttrs, {
  sample: 'block', // "sample" type, which is a block
  abbr: 'inline', // "abbr" type, which is inline
})

This way, we can have inside curlyAttrs() something like this:

if (opts) {
  if (opts[tokens[i].type] && hasCurly(tokens[i].info)) {
    // At this point, the token is custom and the type also contains attributes
    // Here we also know if its inline or block, by just checking opts[tokens[i].type]
  }
}

But here i don’t even know how to continue. Hardcoding the code (copying code from fence block you added lately) will work, but only for block elements and it’s not much flexible.

My idea is, re-factoring this into 2 functions, parseAsInline and parseAsBlock. This two functions will accept the type, and simply return the element with the attributes added correctly. This way you can simply do:

if (opts) {
  if (opts[tokens[i].type] && hasCurly(tokens[i].info)) {
    if (opts[tokens[i].type] === 'block') return parseAsBlock(tokens[i], tokens[i].info)
    return parseAsInline(tokens[i], tokens[i].info)
  }
}

This of course can be also done with 1 simple function like parseElement(type, element, attrs)

parseElement(opts[tokens[i].type], tokens[i], tokens[i].info)

This will make this plug-in fully compatible with other plug-ins that create custom types. I currently have https://github.com/felixsanz/markdown-it-samp, which creates a <pre><samp> element (similar to fenced but samp instead of code).

What do you think of this? I’m sorry that i could not provide a pull request with this implemented, i’m still learning the internals of markdown-it and my implementation will have a lot of bugs…

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:32 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
arve0commented, May 30, 2016

You are correct, if considering the current code/state of markdown-it-attrs. But it’s not impossible. Just needs to find a common ground where to find the {}-curlies. If they are found with a new line at the end: \n{.red}, apply to block, else apply attributes to first inline element. Renderer should take attributes from first inline element and render those to code.

1reaction
felixsanzcommented, May 30, 2016

Example custom token:

Token {
  type: 'sample',
  tag: 'samp',
  attrs: null,
  map: [ 28, 33 ],
  nesting: 0,
  level: 0,
  children: null,
  content: 'test\n',
  markup: '´´´',
  info: ' {.foo}',
  meta: null,
  block: true,
  hidden: false }

This should add class="foo" to <samp>

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Create Custom Types in TypeScript - DigitalOcean
Though the basic types in TypeScript will cover many use cases, creating your own custom types based on these basic types will allow...
Read more >
Documentation - Object Types - TypeScript
Each property in an object type can specify a couple of things: the type, whether the property is optional, and whether the property...
Read more >
TypeScript, Part 3: Working with Custom Types | by Bryn Bennett
When to Use Custom Types · A core object of the application (ie a User , BlogPost , Genre ) that will be...
Read more >
Javascript type of custom object - Stack Overflow
I offer that if you are creating custom objects you can add your own property for type and check against that like so....
Read more >
[@types/react-select] No possibility to use custom objects for ...
[@types/react-select] No possibility to use custom objects for options ... It explicitly states that the options must have a label and value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found