gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.32k forks source link

Debugging Plugin for gatsby-transform-remark #5884

Closed jpyams closed 6 years ago

jpyams commented 6 years ago

Summary

I'm trying to develop a plugin for gatsby-transform-remark to obfuscate contact info from spambots. However, I haven't been able to make the plugin actually affect the page in any way. console.log()s within the Promise don't seem to print out anywhere.

I'm pretty sure I've loaded the plugin properly, since if I add an unmatched brace Gatsby fails with an error.

Is there a way to get printout information from a plugin like this while testing? Or am I doing something else wrong that's preventing the Promise code from running properly?

Is there any documentation on making a plugin for a plugin such as gatsby-transform-remark?

My code (in plugins/gatsby-remark-hide-contact)

index.js

const visit = require(`unist-util-visit`)
const cheerio = require(`cheerio`)
const Promise = require(`bluebird`)

module.exports = ({ markdownAST }, pluginOptions = {}) =>
  new Promise(resolve => {
    visit(markdownAST, `html`, node => {
      const $ = cheerio.load(node.value);
      const link = $('a,object');
      link.each( (index, el) => {
        console.log($(el).text());
        $(el).text('hi');
      });
      node.value = $.html();
    })

    return resolve(markdownAST)
  })
tri613 commented 6 years ago

Hi @jpyams, I think you don't need to wrap it around with promise. I was recently creating a plugin for gatsby-transform-remark too, and I can see my console.log print out in the terminal (not the web browser's console).

Since there is no documentation, I basically take gatsby-remark-prismjs and gatsby-remark-emojis as a reference to develop plugin, hope it helps.

jpyams commented 6 years ago

Thanks @tri613! Gettting rid of the Promise made it so I could see the output and figuring out what was going on.

Looking at the contents of markdownAST, it looks like a plugin is the wrong approach for what I'm doing.