Skip to content

Commit

Permalink
Fix og:image path to be absolute
Browse files Browse the repository at this point in the history
This change resolves the relative note image path to make
an absolute url.

For `blog/post.md` with a `![img](media/image.png)`, the og:image is:
- Before: `siteUrl/media/image.png`
- After:  `siteUrl/blog/media/image.png`
  • Loading branch information
TristanCacqueray committed Sep 30, 2024
1 parent 013be93 commit ceb9ad5
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions emanote/src/Emanote/Model/Note.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Network.URI.Slug (Slug)
import Optics.Core ((%), (.~))
import Optics.TH (makeLenses)
import Relude
import System.FilePath (takeFileName, (</>))
import System.FilePath (takeDirectory, takeFileName, (</>))
import Text.Pandoc (readerExtensions, runPure)
import Text.Pandoc.Builder qualified as B
import Text.Pandoc.Definition (Pandoc (..))
Expand Down Expand Up @@ -368,9 +368,8 @@ parseNoteMarkdown scriptingEngine pluginBaseDir fp md = do
tell [toText $ "Pandoc filter " <> p <> " not found in any of: " <> show pluginBaseDir]
pure Nothing
(x : _) -> pure $ Just x

doc <- applyPandocFilters scriptingEngine filterPaths $ preparePandoc doc'
let meta = applyNoteMetaFilters doc frontmatter
let meta = applyNoteMetaFilters (takeDirectory fp) doc frontmatter
pure (doc, meta)
where
withAesonDefault default_ mv =
Expand All @@ -381,8 +380,8 @@ defaultFrontMatter :: Aeson.Value
defaultFrontMatter =
Aeson.toJSON $ Map.fromList @Text @[Text] $ one ("tags", [])

applyNoteMetaFilters :: Pandoc -> Aeson.Value -> Aeson.Value
applyNoteMetaFilters doc =
applyNoteMetaFilters :: FilePath -> Pandoc -> Aeson.Value -> Aeson.Value
applyNoteMetaFilters fileDirectory doc =
addTagsFromBody
>>> addDescriptionFromBody
>>> addImageFromBody
Expand All @@ -407,7 +406,7 @@ applyNoteMetaFilters doc =
-- `![[foo.jpeg]]` is not handled at all.
addImageFromBody =
overrideAesonText ("page" :| ["image"]) $ \case
B.Image _ _ (url, _) -> [url]
B.Image _ _ (url, _) -> [T.pack (fileDirectory </> T.unpack url)]
_ -> mempty
overrideAesonText :: forall a. (W.Walkable a Pandoc) => NonEmpty Text -> (a -> [Text]) -> Aeson.Value -> Aeson.Value
overrideAesonText key f frontmatter =
Expand Down

0 comments on commit ceb9ad5

Please sign in to comment.