Replace relative links in converted markdown

This commit is contained in:
Fabio Manganiello 2021-11-27 01:19:55 +01:00
parent 1e1bf46f32
commit 2914a74b75
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,8 +1,10 @@
import datetime
import json
import os
import re
import subprocess
import tempfile
from urllib.parse import urlparse
from platypush.plugins import action
from platypush.plugins.http.request import Plugin
@ -29,6 +31,12 @@ class HttpWebpagePlugin(Plugin):
with subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=None) as parser:
return parser.communicate()[0].decode()
@staticmethod
def _fix_relative_links(markdown: str, url: str) -> str:
url = urlparse(url)
base_url = f'{url.scheme}://{url.netloc}'
return re.sub(r'(!?\[.+?])\((/.+?)\)', f'\1({base_url}\2)', markdown)
# noinspection PyShadowingBuiltins
@action
def simplify(self, url, type='html', html=None, outfile=None):
@ -101,6 +109,9 @@ class HttpWebpagePlugin(Plugin):
except Exception as e:
raise RuntimeError('Could not parse JSON: {}. Response: {}'.format(str(e), response))
if type == 'markdown':
self._fix_relative_links(response['content'], url)
self.logger.debug('Got response from Mercury API: {}'.format(response))
title = response.get('title', '{} on {}'.format(
'Published' if response.get('date_published') else 'Generated',