Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow embedding of PDF, video, etc. using image links #351

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/demo/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
2 changes: 1 addition & 1 deletion emanote.cabal
Original file line number Diff line number Diff line change
@@ -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: [email protected]
Expand Down
1 change: 1 addition & 0 deletions src/Emanote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ defaultEmanotePandocRenderers =
PF.urlResolvingSplice
]
[ PF.embedBlockWikiLinkResolvingSplice,
PF.embedBlockRegularLinkResolvingSplice,
PF.queryResolvingSplice
]
inlineRenderers =
Expand Down
30 changes: 19 additions & 11 deletions src/Emanote/Pandoc/Renderer/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down