medium-parser icon indicating copy to clipboard operation
medium-parser copied to clipboard

Handle source code from gist.github.com

Open atilacamurca opened this issue 8 years ago • 13 comments

How do you suggest to handle source code from gist.github.com?

Here's an example post https://medium.com/@lavrton/how-to-optimise-rendering-of-a-set-of-elements-in-react-ad01f5b161ae#.1hjkv2mbg

If we inspect we can see that medium creates a link to https://medium.com/media/9a839b7d6f1167898954c381e0c3b03c?maxWidth=700

This link is an iframe with the contents of the gist.

We can use the gist API https://developer.github.com/v3/gists/#get-a-single-gist

You can get a gist using the url https://api.github.com/gists/07cc0a10c7c96fb1fde4

This return a JSON with details of the gist including a Files object describing the files on the gist. We can iterate this object and generate pre tags.

We can also know the language and enable the highlight! Each file in the object Files has an language associated.

What you think?

atilacamurca avatar May 12 '16 20:05 atilacamurca

Wuuut sounds great! I hadn't thought of that. Yeah medium-parser could totally use that feature :balloon:

gunar avatar May 13 '16 18:05 gunar

Great man, as soon as I have a spare time I will work on that.

I can see great things coming from this lib, keep the great work!

atilacamurca avatar May 13 '16 18:05 atilacamurca

Thank you very much. I'm happy it's useful for you.

If you'd like to discuss any code, open a PR and let's talk about it.

gunar avatar May 13 '16 20:05 gunar

Hey @atilacamurca, did you ever get round to working on this issue? If not, then @gunar I am pretty busy at the moment but would love to see this as a feature in medium-parser, so if I get the time I'll try and open a PR! 🙌

danarth avatar Apr 15 '18 21:04 danarth

not working on it @DanielArthurUK , you should do it :)

atilacamurca avatar Apr 16 '18 13:04 atilacamurca

@atilacamurca @gunar I have some spare time now so I am working on a possible solution to this. I can easily obtain the src of the <iframe> that Medium creates. However, to get the ID of the gist, I will have to asynchronously visit the src url.

Is it okay to add asynchronous calls to the processElement.js script? I just thought that if the processElement script is called for every HTML element on the page, an async call might mess with the script 🤔

This isn't as easy as I first imagined 😂

danarth avatar Jun 05 '18 00:06 danarth

@DanielArthurUK That's not going to work. medium-parser is a synchronous function/library.

You could add a second function (extend the API). This second function would return a Promise. THEN, you can have processElement return an object that the outer function can recognize, do the async call, and replace inline the value.

https://github.com/gunar/medium-parser/blob/2ca35786cd369fbfc947b2b75a380f00d4c4b400/src/index.js#L13

After all that is working, we could open a PR against medium-converter to add this functionality to it.

gunar avatar Jun 07 '18 18:06 gunar

@gunar when you say add a second function, do you mean add a second function after processElement? Surely if we wrap processElement in an asynchronous function, the medium-parser library will still be asynchronous?

Sorry if I am misunderstanding what you are saying! Also I apologise for all of the questions 😅

danarth avatar Jul 17 '18 18:07 danarth

I was just trying to say we could keep both the synchronous and promise-based APIs but looking back it seems irrelevant.

Yeah we need to make medium-parser return a promise with a major version release.

I'll reply to any questions I can! 👍

gunar avatar Jul 22 '18 20:07 gunar

Maybe it is time to think about drafting some ideas for a new major release then? A new promise-based API could be a good idea like you say, plus it would be easier to integrate the GitHub Gist functionality too.

By using promises, medium-parser could be used like so:

fetch('https://medium.com/some-article')
  .then(res => res.text())
  .then(html => parse(html))
  .then(post => {
    // Use the post object as before, e.g. post.markdown, post.title etc
  }) 

The above is just an example. This is a bit off-topic and for another issue probably, just wondered your thoughts on a major change!

danarth avatar Jul 29 '18 21:07 danarth

I have created a new branch DanielArthurUK/medium-parser@next that exposes a promise-based API. I believe this will allow us to easily parse GitHub Gists!!

danarth avatar Jul 31 '18 12:07 danarth

I'm thumbs up on this major change!

gunar avatar Aug 07 '18 14:08 gunar

@gunar That's great news 👍 I am still working on this but if you could create a next branch in the meantime I can open a PR against it 🤓

danarth avatar Aug 07 '18 17:08 danarth