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

Add a filename validation #151

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ You can view the sample pages on this site.
- [x] Dev server
- [x] Color scheme

## Constraints

The filename for pages should follow the pattern below.

`^[a-zA-Z0-9-.\s]+\.md$`

The filename for posts should follow the pattern below.

`^\d{4}-\d{2}-\d{2}-[a-zA-Z0-9-.\s]+\.md$`

## Building and running the app

- Install dependencies: `npm install`
Expand Down
6 changes: 6 additions & 0 deletions src/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ module Misc =
| _ -> Page
| _ -> Page

let isInvalidMarkdownFilenamePattern (s: string) =
Regex.IsMatch (s, @"^[a-zA-Z0-9-.\s]+\.md$") |> not

let isInvalidPostsFilenamePattern (s: string) =
Regex.IsMatch (s, @"^\d{4}-\d{2}-\d{2}-[a-zA-Z0-9-.\s]+\.md$") |> not

type Meta =
{ frontMatter: Parser.FrontMatter option
content: ReactElement
Expand Down
29 changes: 29 additions & 0 deletions src/Generator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,38 @@ module Rendering =
do! IO.writeFile dest page
}

let private checkFilenamePattern (files: string list) =
files
|> List.map IO.leaf
|> List.filter isInvalidMarkdownFilenamePattern
|> function
| [] -> ()
| x ->
x
|> String.concat " "
|> failwithf "Invalid filename patterns: %s"

let private checkPostsFilenamePattern (postRoot: string) (files: string list) =
// TODO: dirty path manipulation.
let postRoot = postRoot.Replace("\\", "/")
files
|> List.filter (fun s -> s.Replace("\\", "/").Contains(postRoot))
|> List.map IO.leaf
|> List.filter isInvalidPostsFilenamePattern
|> function
| [] -> ()
| x ->
x
|> String.concat " "
|> failwithf "Invalid posts filename patterns: %s"

let renderMarkdowns (conf: FrameConfiguration) (site: PathConfiguration) sourceDir destDir =
promise {
let! files = getMarkdownFiles sourceDir

files |> checkFilenamePattern
files |> checkPostsFilenamePattern site.postRoot

let! metas = files |> List.map readSource |> Promise.all
let metas = metas |> Array.filter _.publish

Expand Down
Loading