okf-core-0.14.0.0: src/Okf/ConceptId.hs
-- | Safe concept identifiers for bundle-relative OKF Markdown documents.
module Okf.ConceptId
( ConceptId,
ConceptIdError (..),
conceptIdFromFilePath,
conceptIdToFilePath,
parseConceptId,
renderConceptId,
renderConceptLinkTarget,
renderBundleLinkTarget,
renderConceptLink,
)
where
import Data.Aeson (ToJSON (..))
import Data.ByteString.Char8 qualified as Bytes
import Data.Char qualified as Char
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Network.URI (escapeURIString, isUnreserved)
import Okf.Prelude hiding ((<.>))
import System.FilePath ((<.>))
import System.FilePath qualified as FilePath
-- | A bundle-relative concept path without the @.md@ suffix.
newtype ConceptId = ConceptId
{ segments :: NonEmpty Text
}
deriving stock (Generic, Eq, Ord, Show)
instance ToJSON ConceptId where
toJSON = toJSON . renderConceptId
-- | Why a piece of text could not become a 'ConceptId'.
data ConceptIdError
= EmptyConceptId
| InvalidConceptIdSegment Text
deriving stock (Generic, Eq, Show)
-- | Parse a slash-separated concept identifier such as @tables/users@.
parseConceptId :: Text -> Either ConceptIdError ConceptId
parseConceptId raw =
case Text.splitOn "/" raw of
[] -> Left EmptyConceptId
[""] -> Left EmptyConceptId
firstSegment : rest -> do
parsedSegments <- traverse validateSegment (firstSegment :| rest)
pure (ConceptId parsedSegments)
-- | Render a concept identifier without a file extension.
renderConceptId :: ConceptId -> Text
renderConceptId (ConceptId rawSegments) =
Text.intercalate "/" (NonEmpty.toList rawSegments)
-- | Convert a concept identifier to a bundle-relative Markdown path.
conceptIdToFilePath :: ConceptId -> FilePath
conceptIdToFilePath (ConceptId rawSegments) =
FilePath.joinPath (Text.unpack <$> NonEmpty.toList rawSegments) <.> "md"
-- | Convert a bundle-relative Markdown path to a concept identifier.
conceptIdFromFilePath :: FilePath -> Either ConceptIdError ConceptId
conceptIdFromFilePath path =
parseConceptId (Text.pack (FilePath.dropExtension (FilePath.normalise path)))
-- | The canonical bundle-absolute Markdown link target for a concept, e.g.
-- @/modules/nix-haskell-flake.md@. A link whose URL is this string resolves
-- back to the same 'ConceptId' regardless of which document contains it.
renderConceptLinkTarget :: ConceptId -> Text
renderConceptLinkTarget conceptId =
"/" <> renderBundleLinkTarget (conceptIdToFilePath conceptId)
-- | Render a relative bundle path as a URI reference. Encode the UTF-8
-- bytes of every segment while keeping the path separators.
renderBundleLinkTarget :: FilePath -> Text
renderBundleLinkTarget =
Text.pack . escapeURIString isPathCharacter . Bytes.unpack . Text.encodeUtf8 . Text.pack
where
isPathCharacter character = character == '/' || isUnreserved character
-- | A complete Markdown link to a concept: @[label](/path.md)@. Only the URL is
-- read by OKF link extraction, so an odd label does not break edges, but the
-- caller should choose a label free of unbalanced brackets to keep prose readable.
renderConceptLink :: ConceptId -> Text -> Text
renderConceptLink conceptId label =
"[" <> label <> "](" <> renderConceptLinkTarget conceptId <> ")"
validateSegment :: Text -> Either ConceptIdError Text
validateSegment segment
| Text.null segment || "." `Text.isPrefixOf` segment = invalid
| Text.any (\character -> character == '/' || character == '\\' || Char.isControl character) segment = invalid
| otherwise = Right segment
where
invalid = Left (InvalidConceptIdSegment segment)