From dc78a195341c26e7aa9d3ca3e3e535fd688877dc Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Fri, 23 Sep 2022 15:32:27 -0400 Subject: [PATCH] Allow embedding of PDF, video, etc. using image links (#351) * Support ![]() to embed PDF * remove trace * use embed img syntax * docs: the same syntax works for videos --- docs/demo/embed.md | 6 +++--- emanote.cabal | 2 +- src/Emanote.hs | 1 + src/Emanote/Pandoc/Renderer/Embed.hs | 30 ++++++++++++++++++---------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/demo/embed.md b/docs/demo/embed.md index 5326c9ea2..a8d6b217f 100644 --- a/docs/demo/embed.md +++ b/docs/demo/embed.md @@ -26,19 +26,19 @@ See https://github.com/srid/emanote/issues/24 for progress. Embedding image files as, say, `![[disaster-girl.jpg]]` is equivalent to `![](path/to/disaster-girl.jpg))` (this example links to [[disaster-girl.jpg|this image]]). See also the tip: [[adding-images]]. -[![](disaster-girl.jpg)](https://knowyourmeme.com/memes/disaster-girl) +[![[disaster-girl.jpg]]](https://knowyourmeme.com/memes/disaster-girl) It is also posible to add images inline (example, here's the site favicon: [![[favicon.svg]]]{.w-6}) say in the middle of a paragraph. ### Videos -The following is the result of using `![[death-note.mp4]]`. +The following is the result of using `![[death-note.mp4]]` (note that `![](death-note.mp4)` also works). ![[death-note.mp4]] ### PDFs -PDFs can be embedded using the same syntax; ie. `![[git-cheat-sheet-education.pdf]]` will show: +PDFs can be embedded using the same syntax. The following is the result of using `![[git-cheat-sheet-education.pdf]]` (note that `![](git-cheat-sheet-education.pdf)` also works): ![[git-cheat-sheet-education.pdf]] diff --git a/emanote.cabal b/emanote.cabal index 00ee8cd69..82a6cedd7 100644 --- a/emanote.cabal +++ b/emanote.cabal @@ -1,6 +1,6 @@ cabal-version: 2.4 name: emanote -version: 0.7.9.1 +version: 0.7.10.0 license: AGPL-3.0-only copyright: 2021 Sridhar Ratnakumar maintainer: srid@srid.ca diff --git a/src/Emanote.hs b/src/Emanote.hs index 47d3ac08b..44dc705aa 100644 --- a/src/Emanote.hs +++ b/src/Emanote.hs @@ -143,6 +143,7 @@ defaultEmanotePandocRenderers = PF.urlResolvingSplice ] [ PF.embedBlockWikiLinkResolvingSplice, + PF.embedBlockRegularLinkResolvingSplice, PF.queryResolvingSplice ] inlineRenderers = diff --git a/src/Emanote/Pandoc/Renderer/Embed.hs b/src/Emanote/Pandoc/Renderer/Embed.hs index 58f54530c..46810be4c 100644 --- a/src/Emanote/Pandoc/Renderer/Embed.hs +++ b/src/Emanote/Pandoc/Renderer/Embed.hs @@ -37,7 +37,21 @@ embedBlockWikiLinkResolvingSplice model _nf ctx noteRoute = \case Rel.parseUnresolvedRelTarget parentR (otherAttrs <> one ("title", tit)) url let rRel = Resolve.resolveWikiLinkMustExist model wl RenderedUrl.renderSomeInlineRefWith Resolve.resourceSiteRoute (is, (url, tit)) rRel model ctx inl $ - embedBlockSiteRoute model ctx + either (embedResourceRoute model ctx) (const Nothing) + _ -> + Nothing + +embedBlockRegularLinkResolvingSplice :: PandocBlockRenderer Model R.LMLRoute +embedBlockRegularLinkResolvingSplice model _nf ctx noteRoute = \case + B.Para [inl] -> do + (inlRef, (_, _, otherAttrs), is, (url, tit)) <- Link.parseInlineRef inl + guard $ inlRef == Link.InlineImage + let parentR = R.withLmlRoute R.routeParent noteRoute + (Rel.URTResource mr, _mAnchor) <- + Rel.parseUnresolvedRelTarget parentR (otherAttrs <> one ("title", tit)) url + let rRel = Resolve.resolveModelRoute model mr + RenderedUrl.renderSomeInlineRefWith Resolve.resourceSiteRoute (is, (url, tit)) rRel model ctx inl $ + either (const Nothing) (embedStaticFileRoute model $ WL.plainify is) _ -> Nothing @@ -49,13 +63,7 @@ embedInlineWikiLinkResolvingSplice model _nf ctx noteRoute inl = do (Rel.URTWikiLink (WL.WikiLinkEmbed, wl), _mAnchor) <- Rel.parseUnresolvedRelTarget parentR (otherAttrs <> one ("title", tit)) url let rRel = Resolve.resolveWikiLinkMustExist model wl RenderedUrl.renderSomeInlineRefWith Resolve.resourceSiteRoute (is, (url, tit)) rRel model ctx inl $ - embedInlineSiteRoute model wl - -embedBlockSiteRoute :: Model -> HP.RenderCtx -> Either MN.Note SF.StaticFile -> Maybe (HI.Splice Identity) -embedBlockSiteRoute model ctx = either (embedResourceRoute model ctx) (const Nothing) - -embedInlineSiteRoute :: Model -> WL.WikiLink -> Either MN.Note SF.StaticFile -> Maybe (HI.Splice Identity) -embedInlineSiteRoute model wl = either (const Nothing) (embedStaticFileRoute model wl) + either (const Nothing) (embedStaticFileRoute model $ show wl) runEmbedTemplate :: ByteString -> H.Splices (HI.Splice Identity) -> HI.Splice Identity runEmbedTemplate name splices = do @@ -70,15 +78,15 @@ embedResourceRoute model ctx note = do "ema:note:pandoc" ## pandocSplice ctx (prepareNoteDoc note) -embedStaticFileRoute :: Model -> WL.WikiLink -> SF.StaticFile -> Maybe (HI.Splice Identity) -embedStaticFileRoute model wl staticFile = do +embedStaticFileRoute :: Model -> Text -> SF.StaticFile -> Maybe (HI.Splice Identity) +embedStaticFileRoute model altText staticFile = do let fp = staticFile ^. SF.staticFilePath url = SF.siteRouteUrl model $ SF.staticFileSiteRoute staticFile if | any (`T.isSuffixOf` toText fp) imageExts -> pure . runEmbedTemplate "image" $ do "ema:url" ## HI.textSplice url - "ema:alt" ## HI.textSplice $ show wl + "ema:alt" ## HI.textSplice altText | any (`T.isSuffixOf` toText fp) videoExts -> do pure . runEmbedTemplate "video" $ do "ema:url" ## HI.textSplice url