Skip to content

Commit 476eccf

Browse files
committed
refactor(hickey,lowy): dedup parseNoteRoute via Route.mkLMLRouteFromMdOrOrgFilePath
Catalog.hs and Tools.hs each defined identical `mkLMLRouteFromKnownFilePath Md fp <|> mkLMLRouteFromKnownFilePath Org fp` helpers. The coupling invariant ('both modules agree on what counts as a recognised note path') was structural — adding a third LML format would require two updates with nothing forcing them to agree. Hoist into Route.ModelRoute alongside mkLMLRouteFromKnownFilePath, which encapsulates the same volatility axis (LML-format recognition).
1 parent 553a5c7 commit 476eccf

3 files changed

Lines changed: 9 additions & 15 deletions

File tree

emanote/src/Emanote/MCP/Catalog.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import Emanote.Model (Model)
2727
import Emanote.Model qualified as M
2828
import Emanote.Model.Note qualified as Note
2929
import Emanote.Route qualified as R
30-
import Emanote.Route.Ext (LML (Md, Org))
31-
import Emanote.Route.ModelRoute (mkLMLRouteFromKnownFilePath)
3230
import Emanote.View.Export.Content qualified as ExportContent
3331
import Emanote.View.Export.JSON qualified as ExportJSON
3432
import Optics.Operators ((^.))
@@ -121,7 +119,7 @@ readResource model = \case
121119
MetadataJson ->
122120
pure $ Right $ ResourceBody (decodeUtf8 (ExportJSON.renderJSONExport model))
123121
Note path ->
124-
case parseNoteRoute path >>= (`Note.lookupNotesByRoute` (model ^. M.modelNotes)) of
122+
case R.mkLMLRouteFromMdOrOrgFilePath path >>= (`Note.lookupNotesByRoute` (model ^. M.modelNotes)) of
125123
Nothing -> pure $ Left NotFound
126124
Just note -> do
127125
mContent <- ExportContent.readNoteContent note
@@ -130,7 +128,3 @@ readResource model = \case
130128
Just content ->
131129
let header = ExportContent.generateNoteHeader model note
132130
in Right $ ResourceBody (header <> content)
133-
134-
parseNoteRoute :: FilePath -> Maybe R.LMLRoute
135-
parseNoteRoute fp =
136-
mkLMLRouteFromKnownFilePath Md fp <|> mkLMLRouteFromKnownFilePath Org fp

emanote/src/Emanote/MCP/Tools.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ import Emanote.Model.Note qualified as N
4040
import Emanote.Model.StaticFile qualified as SF
4141
import Emanote.Model.Title qualified as Tit
4242
import Emanote.Route qualified as R
43-
import Emanote.Route.Ext (LML (Md, Org))
44-
import Emanote.Route.ModelRoute (mkLMLRouteFromKnownFilePath)
4543
import MCP.Server (
4644
CallToolResult,
4745
InputSchema (..),
@@ -146,7 +144,7 @@ Returns 'Left' if @path@ isn't a recognised LML source path
146144
-}
147145
getBacklinks :: FilePath -> Model -> Either Text [NoteMatch]
148146
getBacklinks fp model =
149-
case parseNoteRoute fp of
147+
case R.mkLMLRouteFromMdOrOrgFilePath fp of
150148
Nothing -> Left $ "Not a recognised note path: " <> toText fp
151149
Just r ->
152150
Right $ noteMatchOf model . fst <$> G.modelLookupBacklinks r model
@@ -213,7 +211,7 @@ resolveWikilink wlText mFromPath model = do
213211
wl <- maybeToRight ("Not a valid wikilink: " <> wlText) (parseWikiLinkText wlText)
214212
fromR <- case mFromPath of
215213
Nothing -> Right (M.modelIndexRoute model)
216-
Just p -> maybeToRight ("Not a recognised note path: " <> toText p) (parseNoteRoute p)
214+
Just p -> maybeToRight ("Not a recognised note path: " <> toText p) (R.mkLMLRouteFromMdOrOrgFilePath p)
217215
Right $ case Resolve.resolveWikiLinkMustExist model fromR wl of
218216
Rel.RRTFound (Left (_, note)) ->
219217
ResolvedNote (noteMatchOf model (note ^. N.noteRoute))
@@ -257,10 +255,6 @@ resolveWikilinkTool readModel =
257255
-- Helpers
258256
-- ---------------------------------------------------------------------------
259257

260-
parseNoteRoute :: FilePath -> Maybe R.LMLRoute
261-
parseNoteRoute fp =
262-
mkLMLRouteFromKnownFilePath Md fp <|> mkLMLRouteFromKnownFilePath Org fp
263-
264258
-- | Parse a slash-separated wikilink target (e.g. "foo/bar") into a 'WL.WikiLink'.
265259
parseWikiLinkText :: Text -> Maybe WL.WikiLink
266260
parseWikiLinkText s

emanote/src/Emanote/Route/ModelRoute.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Emanote.Route.ModelRoute (
2020
lmlToHtmlRoute,
2121
mkLMLRouteFromFilePath,
2222
mkLMLRouteFromKnownFilePath,
23+
mkLMLRouteFromMdOrOrgFilePath,
2324
lmlSourcePath,
2425
isMdRoute,
2526
-- Static file routes
@@ -160,6 +161,11 @@ mkLMLRouteFromKnownFilePath lmlType fp =
160161
Md -> fmap LMLRoute_Md (R.mkLmlRouteFromFilePath fp)
161162
Org -> fmap LMLRoute_Org (R.mkLmlRouteFromFilePath fp)
162163

164+
-- | Parse a source path as an LML route, trying @.md@ then @.org@.
165+
mkLMLRouteFromMdOrOrgFilePath :: FilePath -> Maybe LMLRoute
166+
mkLMLRouteFromMdOrOrgFilePath fp =
167+
mkLMLRouteFromKnownFilePath Md fp <|> mkLMLRouteFromKnownFilePath Org fp
168+
163169
-- | Source-relative path of an LML note (e.g. @guide/mcp.md@).
164170
lmlSourcePath :: LMLRoute -> FilePath
165171
lmlSourcePath = withLmlRoute R.encodeRoute

0 commit comments

Comments
 (0)