Skip to content

Commit

Permalink
Add mdToHtmlWithOpts()
Browse files Browse the repository at this point in the history
This makes it possible to render IPFS links in md content
with the local HTTP gateway

[deploy]
  • Loading branch information
cipres authored and cipres committed Jun 5, 2023
1 parent 70f803b commit 3bf8099
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ Requirements
============

- python3 >= 3.7 (Works with python *3.7*, *3.8*, *3.9*)
- go-ipfs_ >= 0.5.0 (the installers include go-ipfs version 0.9.0)
- kubo_ >= 0.14.0 (the installers include kubo version *0.17.0*)
- PyQt5 >= 5.13.2
- PyQtWebengine >= 5.13.2
- gstreamer (on Linux) for media player support
Expand Down Expand Up @@ -236,7 +236,7 @@ from the ipfs-logo_ project's repository is included, unchanged.
.. _sponsor: https://gitlab.com/galacteek/galacteek/-/blob/master/SPONSOR.rst
.. _raspberry: https://gitlab.com/galacteek/galacteek/-/blob/master/RASPBERRY.rst
.. _quamash: https://github.com/harvimt/quamash
.. _go-ipfs: https://github.com/ipfs/go-ipfs
.. _kubo: https://github.com/ipfs/kubo
.. _dist.ipfs.io: https://dist.ipfs.io
.. _IPFS: https://ipfs.io
.. _ipfs-logo: https://github.com/ipfs/logo
Expand Down
5 changes: 4 additions & 1 deletion galacteek/did/ipid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,10 @@ async def jsonLdSubjectSigVerify(self, ipfsop,
signature, pubKeyPem
)

return payload.decode('utf-8') == str(subjUri)
if payload:
return payload.decode('utf-8') == str(subjUri)
else:
raise ValueError(f'Cannot verify JWS for subject: {subjUri}')
except Exception as err:
self.message(f'Failed to create signature: {err}')

Expand Down
15 changes: 14 additions & 1 deletion galacteek/dweb/channels/g.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class GHandler(GAsyncObject):
@pyqtSlot(result=int)
def apiVersion(self):
return 9
return 10

@pyqtSlot(str, str)
def logMsg(self, level: str, message: str):
Expand All @@ -40,6 +40,19 @@ def mdToHtml(self, mdText: str):
except Exception:
return ''

@pyqtSlot(str, QJsonValue, result=str)
def mdToHtmlWithOpts(self,
mdText: str,
options: QJsonValue):
try:
opts = options.toVariant()
return markitdown(
mdText,
ipfsLinksUseLocalGw=opts.get('ipfsLinksUseLocalGw', False)
)
except Exception:
return ''

@pyqtSlot(str, result=str)
def urlFromGateway(self, url: str):
app = runningApp()
Expand Down
37 changes: 27 additions & 10 deletions galacteek/dweb/markdown.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import markdown
import platform
from yarl import URL

from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
import xml.etree.ElementTree as etree

from galacteek.core import runningApp

from galacteek.dweb.pygmentedmarkdown import CodeBlockExtension

from galacteek.ipfs.cidhelpers import IPFSPath
Expand All @@ -23,12 +26,13 @@ def mkLink(self, a, label, title, href, parent=None):
el = etree.Element('a')

if '!' in a:
img = etree.SubElement(el, 'img')

img.set('src', href)
img.set('width', '150px')
img.set('height', 'auto')
img.set('title', label)
etree.SubElement(
el, 'img',
src=href,
title=label,
width='50%',
height='auto'
)
elif '%' in a:
vid = etree.SubElement(el, 'video')

Expand Down Expand Up @@ -75,15 +79,23 @@ def link(self, m, ipfsPath):
ipfsPath
)
else:
if '%' in a:
if self.config['useLocalGwUrls'] is True:
# Use galacteek's HTTP gateway

connParams = runningApp().getIpfsConnectionParams()
url = str(ipfsPath.publicUrlForGateway(
URL(connParams.gatewayUrl)
))
elif '%' in a:
url = ipfsPath.dwebUrl
else:
url = ipfsPath.ipfsUrl

return self.mkLink(
a,
linkName if linkName else ipfsPath.objPath,
ipfsPath.objPath, url)
ipfsPath.objPath, url
)


class IPFSPathPattern(Pattern, LinkBuilder):
Expand Down Expand Up @@ -130,6 +142,10 @@ def handleMatch(self, m):

class IPFSLinksExtension(Extension):
def __init__(self, *args, **kwargs):
self.config = {
'useLocalGwUrls': [False,
'Use local HTTP gateway for IPFS obj urls']
}
super(IPFSLinksExtension, self).__init__(*args, **kwargs)

def extendMarkdown(self, md, md_globals):
Expand All @@ -142,10 +158,11 @@ def extendMarkdown(self, md, md_globals):
md.inlinePatterns['ipnspath'] = IPNSPathPattern(self.getConfigs(), md)


def markitdown(text):
def markitdown(text,
ipfsLinksUseLocalGw: bool = False) -> str:
extensions = [
CodeBlockExtension(),
IPFSLinksExtension()
IPFSLinksExtension(useLocalGwUrls=ipfsLinksUseLocalGw)
]

if platform.system() == 'Windows':
Expand Down

0 comments on commit 3bf8099

Please sign in to comment.