From ceb9ad5fda26e35a5870a59443ce063867ac6650 Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Mon, 1 Jul 2024 06:15:00 -0400 Subject: [PATCH] Fix og:image path to be absolute 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` --- emanote/src/Emanote/Model/Note.hs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/emanote/src/Emanote/Model/Note.hs b/emanote/src/Emanote/Model/Note.hs index ebdcd7f39..ffdda4a62 100644 --- a/emanote/src/Emanote/Model/Note.hs +++ b/emanote/src/Emanote/Model/Note.hs @@ -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 (..)) @@ -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 = @@ -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 @@ -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 =