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

Needs a mechanism for replacing elements #7

Open
ticky opened this issue Nov 29, 2016 · 4 comments
Open

Needs a mechanism for replacing elements #7

ticky opened this issue Nov 29, 2016 · 4 comments

Comments

@ticky
Copy link
Owner

ticky commented Nov 29, 2016

Should be possible to replace <a>s with <Link> components

@ticky
Copy link
Owner Author

ticky commented Jan 24, 2017

#8 includes the mechanism by which this would be possible, but I think it’s a thing to look at once the features currently in it have landed!

@wyattanderson
Copy link
Contributor

I accomplished this with markdown-it-modify-token. I have a set of typography components I built with styled-components that I import in each .mdx file as such:

---
imports:
    '{P, H1, H2, H3, H4}': ../Layout
---

Then, I remap any instance of these tags so that the generated JSX uses my tags instead. I think this should also work for a => Link.

const remappedTags = {
  p: 'P',
  h1: 'H1',
  h2: 'H2',
  h3: 'H3',
  h4: 'H4',
};

loader: 'markdown-component-loader',
options: {
  markdownItPlugins: [
    (md) => {
      md.options.modifyToken = (token, env) => {
        if (typeof remappedTags[token.tag] !== 'undefined') {
          token.tag = remappedTags[token.tag];
        }
      };
    },
    require('markdown-it-modify-token'),
  ],
},

What would make this super easy is having a way to inject imports for every file processed by the loader so one wouldn't have to have an import block in each .mdx file.

@ticky
Copy link
Owner Author

ticky commented Oct 23, 2017

That’s clever! I had been considering removing the “implicitly import React” option and replacing it instead with a method for defining imports which apply to all files which are processed. This would (hopefully!) allow for using this with Preact.

The other thing of course is that it’s likely that Markdown Component Loader would need to be able to resolve paths to match where processed files are. I hope that’s not too difficult a job!

I may have to investigate markdown-it-modify-token further as it looks like it could help me accomplish more of what’s going on internally in a simpler manner!

@MathiasSM
Copy link

This would be a great enhancement!

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

No branches or pull requests

3 participants