-
Notifications
You must be signed in to change notification settings - Fork 196
Check markdown rs #5621
base: main
Are you sure you want to change the base?
Check markdown rs #5621
Conversation
Signed-off-by: Domesticcadiz <[email protected]>
Signed-off-by: Domesticcadiz <[email protected]>
…st can see this commit! Signed-off-by: Max Wendlandt <[email protected]>
Signed-off-by: Domesticcadiz <[email protected]>
Signed-off-by: Domesticcadiz <[email protected]>
Speaking to @Christopher-C-Robinson and @Toreseen, we know that the following needs to be done to this draft PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Christopher-C-Robinson and @Toreseen - this is looking good! I've added a few comments.
More generally, I know we've discussed this. But looking at the code I do think it might make sense to split the main into a few other files as this grows. Maybe:
src/output.rs
src/node.rs
src/toc.rs
src/stats.rs
... and then add a corresponding mod output
, mod node
, etc in src/main.rs
to "activate" these modules.
Also, it would be good to add a few unit tests as that will help shake out bugs and increase your confidence in the functions as you'll know they can tolerate all types of nasty input (writing tests can be fun as you try to find lots of devious inputs for your functions to handle 😈)
cmd/check-markdown-rs/src/main.rs
Outdated
let input = fs::read_to_string(input_file_path)?; | ||
let root = parse_document(&arena, &input, &ComrakOptions::default()); | ||
|
||
let toc = generate_toc(&root, &arena); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to only display this when the toc
subcommand (or an option) is specified. We use a sub-command for the existing golang tool:
You'll want to cargo add clap
and then ideally create structs and/or enums to represent, declaratively, the CLI options. For examples, see:
cmd/check-markdown-rs/src/main.rs
Outdated
eprintln!("Usage: {} <input_file>", args[0]); | ||
} | ||
|
||
let input_file_path = env::current_dir()?.join("src").join(args[1].clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bit odd: why do we need to add src
? Although it might work with cargo run -- $file
, will it also work if you cargo install
and then run check-markdown-rs "$file"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure if this is what you are lookin for, bit I'm pushing a commit now that removes the SRC from the path, assuming that you will already cd into the src directory.
cmd/check-markdown-rs/src/main.rs
Outdated
NodeValue::Heading(heading) => println!( | ||
"{}Header (level {}): {}", | ||
indent, | ||
heading.level, | ||
get_node_text(&node) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know these blocks are small, but I'd be inclined to create a set of functions to handle each node type:
handle_heading_node(indent, level, text)
handle_text_node(indent, text)
handle_link_node(indent, title, url)
The advantages of doing this include:
- simplifies
print_nodes()
and makes it easier to read. - makes it easier to unit test different types of nodes raising confidence in the parts of the code you have tested formally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I added what you were looking for.
cmd/check-markdown-rs/src/main.rs
Outdated
return Err(String::from("Error: More than one H1 heading found.")); | ||
} | ||
} | ||
|
||
if level > last_seen_level + 1 { | ||
return Err(String::from("Error: Invalid heading level order.")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Can you remove the
Error:
prefix from the errors? The type of these objectsResult::Err
already encodes that they are errors so it's best to let the higher levels handle adding prefixes like this (see comment on that topic below). - Can you include the heading in the error being returned here? In fact, ideally, you'd also add the line number and the filename to the error (either here or at a higher level) to make it 100% clear to the user which part of which file contains the error. This is particularly important when the tool starts to jump between files when it finds a cross-file link, and then finds, for example, an invalid link in another file with the same name as a link in the initial file (yep - I've been there and done that ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first part of this should be fixed now!
cmd/check-markdown-rs/src/main.rs
Outdated
println!(" Found {} invalid links:", errors.len()); | ||
for error in errors { | ||
println!(" {}", error); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the way you've collected all the errors together here and are displaying them at the end. Conventionally, errors should be displayed to standard error stream, so can you change the println!()
calls to eprintln!()
and add an "ERROR: "
prefix to the call in the for loop?
Signed-off-by: Max Wendlandt <[email protected]>
Signed-off-by: Max Wendlandt <[email protected]>
Signed-off-by: Maxwell Wendlandt <[email protected]>
Signed-off-by: Domesticcadiz <[email protected]>
Signed-off-by: Maxwell Wendlandt <[email protected]>
cmd/check-markdown-rs/README.md
Outdated
cargo build --release | ||
./target/release/markdown_parser input_file.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It might be safe to install the command to avoid the target line potentially being incorrect:
$ cargo install --path .
$ check-markdown-rs input_file.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good!
cmd/check-markdown-rs/src/main.rs
Outdated
if args.len() < 2 { | ||
eprintln!("Usage: {} <input_file>", args[0]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- You need to call
std::process::exit(1)
after displaying the message: currently, the message is displayed but the program keeps running, then fails:$ check-markdown-rs Usage: check-markdown-rs <input_file> thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src/main.rs:22:52 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
- Please can you add some argument parsing code so that the user can run:
$ check-markdown-rs --help $ check-markdown-rs --version
…curred in the markdown doc Signed-off-by: Maxwell Wendlandt <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Christopher-C-Robinson and @Toreseen,
As we're very close to the end of the outreach project now, I suggest you rework this PR to remove the binary files, etc as follows. Note that once you've done this though, @Toreseen won't be able to git pull
from @Christopher-C-Robinson's fork, so you'll both have to work together and have just @Christopher-C-Robinson git push -f
to his fork once you've followed the steps below. The steps below are similar in principle to what we did in the meeting on Tuesday with Alec and Jenny's branch. Yes, this is "hacky" in the sense that we're not using git
to resolve the problems - we're simply recreating the branch, but I think this approach is simpler to understand. As always, make sure you have a copy of your local branch/repo before doing the following:
# Update your copy of the main branch
git checkout main
git pull
# Switch to your feature branch
git checkout check-markdown-rs
# Save the files you've created for this new tool
cp cmd/check-markdown-rs/Cargo.toml cmd/check-markdown-rs/README.md cmd/check-markdown-rs/src/main.rs /tmp/
# Rename the original version of the branch (it's now a "backup")
git branch -m check-markdown-rs-original
# Create a new clean version of the feature branch
git checkout main
git checkout -b check-markdown-rs
# Recreate the branch
mkdir cmd/check-markdown-rs
cd cmd/check-markdown-rs
cp /tmp/Cargo.toml /tmp/README.md .
mkdir src
cp /tmp/main.rs src
# Add the new files to git
git add Cargo.toml README.md src/main.rs
# Save the changes
git commit -as
# Update your fork (which will automatically update this PR)
git push -f
Once you've done that, you'll also need to update cmd/README.md
to add an entry for your new tool, so:
- Add an entry for your new tool to
cmd/README.md
git commit -as
- Merge the new commit with the main commit by running
git rebase -i main
. - Change the 2nd commit to
squash
. - Save and quit the editor.
- Update the PR again by running,
git push -f
@@ -1,13 +0,0 @@ | |||
# CLI tools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Christopher-C-Robinson and @Toreseen - I don't think this file should have been deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The instructions above show you how to recreate the entire branch which will resolve this issue 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that now. I added that back in. should be pushed sometime tonight.
@Toreseen and I have done some serious work tonight on our code. If you get a chance, can you take a look at it. Might be messy bit it works. We both spent about 12 hours each on it individually. I’m finally off to bed. Hopefully we are getting close! |
Signed-off-by: Maxwell Wendlandt <[email protected]>
… function. Signed-off-by: Maxwell Wendlandt <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Christopher-C-Robinson and @Toreseen ! This is very close now.
Before we can get this landed though, you'll need to clean up the branch to (a) remove all the binary files; and (b) collapse all your commits into a single one - see my big "branch recreation" comment.
cmd/check-markdown-rs/src/main.rs
Outdated
eprintln!("Usage: {} <input_file>", args[0]); | ||
return Ok(()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Ideally, this would return an Err
as this is it invalid to call the tool without specifying a file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is fixed now
@jodh-intel , I split the main.rs into different files. let me know what your thoughts are. I will work more on it tonight. |
Signed-off-by: Maxwell Wendlandt <[email protected]>
…ther requested freatures. Signed-off-by: Maxwell Wendlandt <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Christopher-C-Robinson and @Toreseen - this is looking good.
Could you remove the binary files from this PR now? Also, could you delete the unused functions that cargo build
lists?
Draft of markdown parser