Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

ScalabeginnercliAI generated

Markdown Word and Character Counter

Build a Scala command-line tool that scans a folder of Markdown files, such as a project's documentation or your notes, and reports for each file its number of words and characters, how many headings it has at each level, and how many links it contains, plus totals for the whole folder. It uses regular expressions to recognize Markdown syntax and skips code blocks so code isn't counted as text. It's a good first Scala project: you'll learn reading files and walking folders, regular expressions with scala.util.matching.Regex, case classes to hold results, combining results with folds, formatting a table, and testing with munit.

Estimate
~6h
Steps
5
Completed by
0
Proposed by
codeseed.app

sbt · scala.util.matching.Regex

Project roadmap

  1. 01

    Read the Markdown files

    ~1h

    Create an sbt project with munit. Take a folder as a command-line argument, check it exists, and find all .md files in it and its subfolders (with java.nio.file.Files.walk, closing the stream properly). Read each file as UTF-8, reporting files that can't be read and continuing. Done when: the tool lists every Markdown file in a test folder.

  2. 02

    Find headings and links with regex

    ~1.5h

    Write regular expressions for headings (lines starting with 1 to 6 # followed by a space) and for links ([text](url), not counting images, which start with !), and use findAllIn and pattern matching to extract them. Skip lines inside fenced code blocks (between ``` lines) with a simple flag. Done when: unit tests show headings by level and links are found correctly, and code blocks are ignored.

  3. 03

    Collect the statistics

    ~1h

    Define a case class FileStats (path, words, characters, headings per level as a Map and links) and compute it for each file: words are runs of letters and digits outside code blocks, and characters exclude line breaks. Add a method to combine two FileStats, and use foldLeft to compute the totals. Done when: totals equal the sum of the per-file statistics.

  4. 04

    Print a summary table

    ~1h

    Print a table with a row per file (relative path, words, characters, headings and links), sorted by word count, with columns aligned using padTo or formatted strings, and a totals row. Add --sort name|words and --json options. Done when: the table is aligned and the options work.

  5. 05

    Write unit tests

    ~1.5h

    Write munit tests with sample Markdown strings: headings of each level (and # without a space, which isn't a heading), links and images, links inside code blocks, empty files, and combining statistics. Add a test that runs over a temporary folder with a few files. Done when: sbt test passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~6h · 5 steps

Tech stack

sbtscala.util.matching.Regex