Skip to content

Fix heading links in nested pages #419

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

Merged
merged 4 commits into from
Sep 7, 2017
Merged

Conversation

behnam
Copy link
Contributor

@behnam behnam commented Sep 2, 2017

Plus fixing the whitespace chars not being replaced by hyphen.

Also expand tests for link creations, and add test for nested pages.

Fixes https://github.com/azerupi/mdBook/issues/416
Fixes https://github.com/azerupi/mdBook/issues/417

And, also fix some test warnings by moving non-test module files into their own directories.

@azerupi
Copy link
Contributor

azerupi commented Sep 2, 2017

Thanks for the PR!
I did a very quick review pass and it looks very good for most parts! Travis fails because of rustup, but appveyor seems to run the tests and fail at check_correct_cross_links_in_nested_dir. I will do a more in depth review soon. :)

@behnam
Copy link
Contributor Author

behnam commented Sep 2, 2017

Uh, on windows the filepath needs to be changed to use forward-slash, instead of backslash. I'll submit an update.

@behnam behnam force-pushed the nested branch 4 times, most recently from d7ac12d to a4533cf Compare September 2, 2017 20:41
@behnam
Copy link
Contributor Author

behnam commented Sep 2, 2017

Travis is completely broken right now. I filed this: https://github.com/azerupi/mdBook/issues/421

Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan left a comment

Choose a reason for hiding this comment

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

Looks pretty good 👍

Breaking out the dummy book creation and helpers into two separate modules is a good idea, seeing as they're separate and the helpers module will probably expand as we add more tests.

Note to self: update #422 when this merges

@@ -457,7 +458,7 @@ fn id_from_content(content: &str) -> String {
let mut content = content.to_string();

// Skip any tags or html-encoded stuff
let repl_sub = vec![
static REPL_SUB: &[&str] = &[
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming you meant const REPL_SUB ... here. Using static means it actually gets embedded in the binary and is typically only done for global mutable objects or if your hardware requires it (like the exception vector when writing a kernel).

I'd also pull it up to the top of the file and name it to something like HTML_ENCODE_BLACKLIST (feel free to give it a better name) so it's easy to update later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooop! Right.

"first/nested.html",
"second.html",
"conclusion.html",
r#"href="intro.html""#,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice fix! Without the href=" this test wouldn't have actually been checking for links, just that the filenames are present, which means it's not actually testing what we want it to.

src/lib.rs Outdated
@@ -100,6 +100,8 @@ pub use book::BookItem;
pub use renderer::Renderer;

/// The error types used through out this crate.
// TODO: Drop after error_chain is fixed
#[allow(unused_doc_comment)]
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be pulled out into its own PR. error-chain has actually fixed this, so bumping the dependency version to 0.11.0-rc.2 would be sufficient.

Copy link
Member

Choose a reason for hiding this comment

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

yeah; I did this on one of my recent projects

let rendered = self.post_process(rendered,
filename.file_name().unwrap().to_str().unwrap_or(""),
&normalize_path(filepath.to_str()
.expect(&format!("Bad file name: {}", filepath.display()))),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it a good idea to panic here? Another option may be to assume a safe default or return an error.

If we were to return an error, I imagine it'd look something like this:

filepath.to_str()
  .ok_or_else(Error::from("Bad file name: {}", filepath.display()))?`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I would expect non-Unicode file names would be caught somewhere earlier in the lib, but in case they are not, makes sense to return a nice error here.

@Michael-F-Bryan
Copy link
Contributor

I just had another look through this PR... Are there any tests to make sure normalize_id() correctly translates titles containing spaces get converted to using hyphens?

It might also be useful to add an integration test in tests/rendered_output.rs to make sure the chapter title Chapter 1 gets a hyphenated id (i.e. <h1 id="chapter-1">Chapter 1</h1>).

Move non-test test module files into their own directories to prevent
cargo from running them as tests. Then suppress the left-over warnings.

Move *dummy book* code and data into a shared folder, and leave the rest
of helper utilities (one function) in the original module.
Plus fixing the whitespace chars not being replaced by hyphen.

Also expand tests for link creations, and add test for nested pages.

Fixes <https://github.com/azerupi/mdBook/issues/416>
Fixes <https://github.com/azerupi/mdBook/issues/417>
On the web, the normalized path separator is forward-slash (`/`), so we
use the built-in `is_separator()` method to replace any path separator
with the forward-slash, to ensure consistent output on unix and windows
machines.
@behnam
Copy link
Contributor Author

behnam commented Sep 6, 2017

I just had another look through this PR... Are there any tests to make sure normalize_id() correctly translates titles containing spaces get converted to using hyphens?

Yes, the newly added "## Some Section" to the nested first pages actually tests SPACE being converted to HYPHEN, and in fact makes sure both href and id are set correctly.

I'm submitting a fix for the inline comments, and I think it would be good to land.

@azerupi
Copy link
Contributor

azerupi commented Sep 6, 2017

Thank you for fixing the issues raised by @Michael-F-Bryan !
I will try to take a last look at this PR tonight to merge it ASAP.

@steveklabnik
Copy link
Member

Awesome, and then we should cut a release.

@azerupi azerupi merged commit f4513d3 into rust-lang:master Sep 7, 2017
@azerupi
Copy link
Contributor

azerupi commented Sep 7, 2017

Thanks a lot for this PR @behnam!
I will publish a new release with the fixes in a couple of minutes.

@behnam behnam deleted the nested branch September 7, 2017 20:53
Ruin0x11 pushed a commit to Ruin0x11/mdBook that referenced this pull request Aug 30, 2020
Fix heading links in nested pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants