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

enable plugins with transforms #305

Merged
merged 9 commits into from
Sep 2, 2019

Conversation

frankieali
Copy link
Contributor

Allowing remark to execute parser.runSync() so that plugin transforms can be applied to the AST tree. Demo here: https://frankieali.github.io/react-markdown/demo/dist/

Closes #188

@frankieali
Copy link
Contributor Author

I noticed that lists were not rendering as expected when I dropped remark-toc in for testing, since it was part of the original issue in #188. The renderer conditions were looking for null values but not undefined so I added them. It's not exactly related to enabling plugins with transform but it does relate to #188 so let me know if this should be separated out into a different PR.
I've updated my demo above with remark-TOC

@callain
Copy link

callain commented May 22, 2019

@frankieali Will this also works for plugin like remark-gemoji-to-emoji ? If this is the case I might need your patch 👍

@frankieali
Copy link
Contributor Author

@callain Yes, my demo worked fine when I dropped remark-gemoji-to-emoji in.
I'm deep in another branch right now, but attached is a screen shot.

gemoji-to-emoji-example

@callain
Copy link

callain commented May 31, 2019

@frankieali Cool !
@rexxars Do you have a moment to check this PR and make a version if everything is ok ?

Copy link
Collaborator

@rexxars rexxars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks, and the sourcePosition case change was unintentional, I assume?
Will also want to add some tests for the plugin changes.

if(node.position === undefined) {
visit(root, node, (nodeVisit, indexVisit, parentVisit) => {
// just in case parent does not have position either
const defaultPosition = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since defaultPosition is static, maybe hoist it to the top of the file?


function astToReact(node, options, parent = {}, index = 0) {
const renderer = options.renderers[node.type]

if(node.type === 'root') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but coding style wants a space between the if and the parentheses opening

@@ -47,7 +67,7 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
}

if (opts.rawSourcePos && !isTagRenderer) {
props.sourcePosition = node.position
props.sourceposition = node.position
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this accidental? Why was this changed?

@frankieali
Copy link
Contributor Author

I've made the requested updates. React was complaining once that sourcePosition should be sourceposition on my local, but I didn't look into it enough to understand why. I've reverted it back.

As far as the tests go, I'm seeing one failure on rawSourcePos, but it seems more like a fix to me since the rawSourcePos snapshot is undefined and now the test is returning a position object.

should pass on raw source position to non-tag renderers if rawSourcePos option is enabled

    expect(received).toMatchSnapshot()

    Snapshot name: `should pass on raw source position to non-tag renderers if rawSourcePos option is enabled 1`

    - Snapshot
    + Received

    - undefined
    + Position {
    +   "end": Object {
    +     "column": 6,
    +     "line": 1,
    +     "offset": 5,
    +   },
    +   "indent": Array [],
    +   "start": Object {
    +     "column": 1,
    +     "line": 1,
    +     "offset": 0,
    +   },
    + }

@callain
Copy link

callain commented Jun 17, 2019

@rexxars Are the changes okay to you ?

@rexxars
Copy link
Collaborator

rexxars commented Jun 17, 2019

There are still no tests for the new functionality - adding a minor plugin to the test suite to ensure that the transformation step is actually is executed would be nice. I'm not sure if there are good reasons why the nodes without a position needs one - if they do, maybe add a comment as to why, and include some tests to ensure that the position gets assigned as expected?

root is also assigned in the global module scope - I think modifying the astToReact signature a bit would be better, ensuring that the root is always explicit (the root-level node passed from react-markdown.js). Also not sure why it's assigned to an empty string by default, this seems like an odd choice.

@callain
Copy link

callain commented Jun 30, 2019

@frankieali Hello, can you take @rexxars feedback in consideration ?

@frankieali frankieali force-pushed the 188-plugins-with-transforms branch from 7070777 to 24caab5 Compare July 3, 2019 21:07
@frankieali
Copy link
Contributor Author

I've rebased my PR to include the latest in master. I've added some dev comments to the code and added the Table of Contents plugin as a dev-dependency for testing and demo purposes.

The reason for the root variable in the global module scope is to cache the root node so it can be walked later when trying to get position attributes from ancestor nodes. Code throughout the ast-to-react.js file make references to the node.position attribute. Tacking the parent's positioning onto child nodes seems the least obtrusive way to resolve this issue.

@rexxars
Copy link
Collaborator

rexxars commented Jul 4, 2019

Thanks, I'll have a look!

@simoncorry
Copy link

+1 for implementing this!

@EricWVGG
Copy link

I gave this PR a try with about five different plugins last night and so far it's working fabulously.

@cheshire137
Copy link

This pull request is working for me to get the gemoji and gemoji-to-emoji plugins working!

@callain
Copy link

callain commented Aug 10, 2019

Hello @rexxars , hope you're good :) Can you review/merge this PR ? 😄

@michelhelms-tdg
Copy link

Status? I would love to see that merged and the requested changes seem very minor.

@rexxars
Copy link
Collaborator

rexxars commented Aug 30, 2019

Sorry, just moved to another country, been quite hectic. Will take a look at PRs this weekend.

@rexxars rexxars merged commit c63dccb into remarkjs:master Sep 2, 2019
@rexxars
Copy link
Collaborator

rexxars commented Sep 2, 2019

Thanks so much for this! Released in 4.2.0

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

Successfully merging this pull request may close these issues.

Plugins are not working
7 participants