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

fix: Codec for article slugs #128

Merged
merged 3 commits into from
Mar 23, 2024
Merged

Conversation

pete-murphy
Copy link
Contributor

Allow for non-alphanumeric and non-lower-case characters in slugs.

Fixes #127.

Comment on lines 21 to 30
-- | A `Slug` represents a string value which is guaranteed to have the
-- | following qualities:
-- |
-- | - it is not empty
-- | - it consists of groups of characters separated by `-` dashes,
-- | where the slug cannot begin or end with a dash, and there can
-- | never be two dashes in a row.
-- |
-- | Example: `Slug "this-is-an-article-slug"`
newtype Slug = Slug String
Copy link
Contributor Author

@pete-murphy pete-murphy Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied from https://github.com/thomashoneyman/purescript-slug/blob/0142716b49758829d5b48244fe1e9f87ea935b77/src/Slug.purs#L21-L31, removing the following guarantees: "every character with a defined notion of case is lower-cased" and "it consists of alphanumeric groups of characters".

-- Replace non-Latin 1 characters with spaces to be stripped later.
onlyAlphaNum =
fromCodePointArray
<<< map (\x -> if isLatin1 x then x else codePointFromChar ' ')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/Data/Slug.purs Outdated Show resolved Hide resolved
-- | ```
generate :: String -> Maybe Slug
generate s = do
let arr = words $ onlyLatin1 $ stripApostrophes s
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow for non-alphanumeric and non-lower-case characters in slugs.

Fixes thomashoneyman#127.
derive newtype instance Semigroup Slug

instance Show Slug where
show (Slug str) = "(Slug " <> str <> ")"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should ditch these instances since they aren’t used here (I generally don’t use Show, and we use codecs instead of JSON instances)

Copy link
Contributor Author

@pete-murphy pete-murphy Mar 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the *Json, Show, and Semigroup instances (leaving Eq and Ord since those are used in instance ordRoute :: Ord Route).

truncate n (Slug s)
| n < 1 = Nothing
| n >= String.length s = Just (Slug s)
| otherwise = generate $ String.take n s
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| otherwise = generate $ String.take n s
| otherwise = generate $ String.take n s

@thomashoneyman
Copy link
Owner

Thanks! I’m fine integrating this module into the code base to fix this issue. Looks like we should also remove the import from Spago?

@pete-murphy
Copy link
Contributor Author

Looks like we should also remove the import from Spago?

Will do 👍 I'm surprised there's no warning from spago build about unused dependencies, maybe that's not a feature in new spago yet?

@pete-murphy
Copy link
Contributor Author

pete-murphy commented Mar 23, 2024

Also, I see

Lockfile is out of date, generating it...
Lockfile written to spago.lock. Please commit this file.

when running spago build, but *.lock in .gitignore ignores it, should there be an exception for this spago.lock file?

Edit: I added it here ced03b5 but I can remove that commit if we don't want to track spago.lock in Git.

Removing unused `Semigroup`, `Show`, `EncodeJson`, and `DecodeJson`
instances from `Slug` module, adding newline at end of file, and
removing unused `slug` dependency from `spago.yaml`.
@pete-murphy
Copy link
Contributor Author

I’m fine integrating this module into the code base to fix this issue.

I created an issue in slug in case there is interest in an upstream fix thomashoneyman/purescript-slug#3

@thomashoneyman
Copy link
Owner

We can use the —pedantic-packages flag to get warnings for unused dependencies. And yes, the lock being ignored is no longer relevant, we do want the spago lock committed!

@thomashoneyman thomashoneyman merged commit c3f1f70 into thomashoneyman:main Mar 23, 2024
1 check passed
@thomashoneyman
Copy link
Owner

I merged this because it fixes the existing issue — but if you have follow up things you’d like to add I’m all ears!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Global Feed shows "Articles not yet loaded"
3 participants