Skip to content

Commit

Permalink
Merge pull request #151 from krymtkts:feature/check-invalid-date
Browse files Browse the repository at this point in the history
Add a filename validation
  • Loading branch information
krymtkts authored Feb 10, 2024
2 parents 23ff6a2 + bbf13ae commit 07bb11f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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

0 comments on commit 07bb11f

Please sign in to comment.