trentm / python-markdown2

markdown2: A fast and complete implementation of Markdown in Python
Other
2.66k stars 433 forks source link

Extra for generating link preview #488

Closed BoPeng closed 1 year ago

BoPeng commented 1 year ago

Instead of using a node.js based solution (e.g. link-preview-generator), I am wondering how I can generate link preview during the processing of markdown. There appears to modules (e.g. linkpreview) that perform the actual uplifting so maybe it is not too difficult to add an extra for this.

BoPeng commented 1 year ago

I notice that extras are not implemented as separate modules, so this has to be incorporated into lib/markdown2.py. Does this look like a decent feature to justify the acceptance of a PR?

nicholasserra commented 1 year ago

Where do you see this preview information being stored?

There's also pre and post process hooks in the codebase if you wanna import and then subclass the module, then add your own data as you see fit.

Also check out https://github.com/michaelhelmick/lassie/

BoPeng commented 1 year ago

Thanks for pointing me to lassie. Basically I want to process markdown (or generated HTML content) and add previews. It has not been decided yet but I think I want to convert paragraphs with a single link to a block with image, title etc. Something like

I see

https://www.cnn.com/2022/12/04/us/power-outage-moore-county-criminal-investigation/index.html

today

will be converted to something like

I see

[ image ] Title
[ image [ author, date, etc

today
nicholasserra commented 1 year ago

Got it. Sounds interesting. But also very specific. Would probably be a worthwhile extra assuming it was built out to allow customization. Not sure if that's worth it for you to work on, but if it were to go down that path, it would probably be accepted.

BoPeng commented 1 year ago

Sounds interesting. But also very specific ... built out to allow customization.

I am using python-markdown2 to convert user input into HTML in the backend of a website. I am calling markdown2 like

markdown(my_text, extras= { ...})

so I essentially need a hook like

def link_to_preview(link):
    return html-based-on-lassie

markdown(my_text, extras={
    'link_preview': {
        'select': 'single-paragraph',
        'processor': link_to_preview
    }
 }

So that markdown2 will look for particular types of links (in this case single-link-paragraphs) and replace them with results from a user-provided processor.