Skip to content

Pages Refactor - #1504

Merged
waylan merged 23 commits into
mkdocs:masterfrom
waylan:pages
Jun 28, 2018
Merged

waylan merged 23 commits into
mkdocs:masterfrom
waylan:pages

Conversation

@waylan

@waylan waylan commented Jun 8, 2018

Copy link
Copy Markdown
Contributor

Probably still needs some tweaks, but I believe this is fully functional. I wanted to get this in front of others for review, especially third party theme authors (@squidfunk, @notpushkin, @chrissimpkins, @hfagerlund, @cjsheets, @michaeltlombardi).

Some of the backward incompatible changes do not have a graceful upgrade path, but the underlying structure is so drastic that its really any way to support both the old and new. As this is intended to be included in the 1.0 release, there is no requirement to maintain compatibility (we are making a major release) and we need to get this correct now as we will be stuck with it throughout the 1.x release cycle.

The documentation changes and release notes provide a decent summary of the changes made. I've copied the release notes below. Some additional information, including notes regarding some of the decisions made are included in the commit message for the second commit. The first commit is from @tomchristie's work in #689, which this builds off of.

Internal Refactor of Pages, Files, and Navigation

Internal handling of pages, files and navigation has been completely refactored.
The changes included in the refactor are summarized below.

Backward Incompatible Changes

As part of the internal refactor, a number of backward incompatible changes have
been introduced, which are summarized below.

URLS have changed when use_directory_urls is False

Previously, all Markdown pages would be have their filenames altered to be index
pages regardless of how the [use_directory_urls] setting was configured.
However, the path munging is only needed when use_directory_urls is set to
True (the default). The path mungling no longer happens when
use_directory_urls is set to False, which will result in different URLs for
all pages that were not already index files. As this behavior only effects a
non-default configuration, and the most common user-case for setting the option
to False is for local file system (file://) browsing, its not likely to
effect most users. However, if you have use_directory_urls set to False
for a MkDocs site hosted on a web server, most of your URLs will now be broken.
As you can see below, the new URLs are much more sensible.

Markdown file Old URL New URL
index.md index.html index.html
foo.md foo/index.html foo.html
foo/bar.md foo/bar/index.html foo/bar.html

Note that there has been no change to URLs or file paths when
use_directory_urls is set to True (the default), except that MkDocs more
consistently includes an ending slash on all internally generated URLs.

The pages configuration setting has been renamed to nav

The pages configuration setting is deprecated and will issue a warning if set
in the configuration file. The setting has been renamed nav. To update your
configuration, simply rename the setting to nav. In other words, if your
configuration looked like this:

pages:
    - Home: index.md
    - User Guide: user-guide.md

Simply edit the configuration as follows:

nav:
    - Home: index.md
    - User Guide: user-guide.md

In the current release, any configuration which includes a pages setting, but
no nav setting, the pages configuration will be copied to nav and a
warning will be issued. However, in a future release, that may no longer happen.
If both pages and nav are defined, the pages setting will be ignored.

Template variables and base_url

In previous versions of MkDocs some URLs expected the [base_url] template
variable to be prepended to the URL and others did not. That inconsistency has
been removed. All URLs must now be joined with the base_url. As previously, a
slash must be included between the base_url and the URL variable. For example,
a theme template might have previously included a link to the site_name as:

<a href="{{ nav.homepage.url }}">{{ config.site_name }}</a>

And MkDocs would magically return a URL for the homepage which was relative to
the current page. That "magic" has been removed and the base_url must now be
explicitly included:

<a href="{{ base_url }}/{{ nav.homepage.url }}">{{ config.site_name }}</a>

This change applies to any navigation items and pages, as well as the
page.next_page and page.previous_page attributes. For the time being, the
extra_javascript and extra_css variables continue to work as previously
(without base_url), but they have been deprecated and the corresponding
configuration values (config.extra_javascript and config.extra_css
respectively) should be used with base_url instead.

Any URL variables which should not be used with base_url are explicitly
documented as such.

lovelydinosaur and others added 2 commits May 9, 2018 12:01
This is only the contents of the 'structure' dir from mkdocs#689.
Still needs to be implemented in the build process after being updated.
Some notes regarding various decisions made here:

And paths which start with a slash are assumed to be relative to the
docs_dir root. This behavior fixes mkdocs#192. However, the slash not being
present in the output may surprise some users who are trying to create a
link relative to the server root when the mkdocs root is not at the
server root.

Still need to address links to files from the theme. I note that in the
homepage of our docs we link to `/img/favicon.ico`, which is a file
provided by the theme. It appears that the previous behavior was to
treat links which started with a slash as not local links and therefore
a check was not necessary. This then provided a workaround to avoid the
warning when linking to theme provided files.

* Page.url is the url relative to the site_dir
* Page.canonical_url is the relative url joined with site_url or None if
  site_url is not defined (the default).
* Page.abs_url is the path component of the canonical url or None if
  canonical_url is None.

Note that new behavior is slightly different than before. Previously
abs_url ignored site_url and was always url with '' prepended. With the
new behavior, is site_url includes a subdir, that subdir will be
included in the abs_url.

When not on a server, there is no sensable "absolute_url" for a page.
Therefore, we shouldn't try to define one.

The thinking is that users generating docs to be browsed in the local
file system (`file://`) should leave the site_url setting unset, while
users who will be serving their docs from a server should be setting the
site_url. And if the site_url point sot a subdir of the server, the
abs_url will stil be absolute from the server root as it uses the "path"
of the canonical_url of the page.

Note that without the magical url context all URLs must be prepended by
`{{ base_url }}/` in the templates. While this requires a change in
third party themes, it is more consistent.

The rest of the tests should probably be updated to use this.

Also load nav from files in get_navigation() rather than in config
validation. That way we only walk docs_dir once.

Note that the two failing tests need to be moved to the nav_tests to
ensure get_navigation() returns the proper nav structure when no nav
config is provided and the file structure is relied upon.

And add `files` keyword to `nav` event. Previously pages and nav were
synonymous. As that is no longer the case, a plugin may need access to
the files collection to properly manipulate the nav.
@chrissimpkins

Copy link
Copy Markdown

Thanks for the summary and poke. Much appreciated! I will have a look at this over the weekend. What is time frame for 1.0 rollout?

@squidfunk

Copy link
Copy Markdown
Contributor

Sounds great and looks good to me!

@waylan

waylan commented Jun 8, 2018

Copy link
Copy Markdown
Contributor Author

The time frame for 1.0 is "when it's done." This PR should resolve about half of the open issues on the 1.0 milestone. Most everything else is just housekeeping type stuff, so I expect by the end of summer. That said, this is being worked in in my spare time so no guarantees.

@chrissimpkins

Copy link
Copy Markdown

Wasn't a criticism, rather it was to place changes on our end on my TODO list radar in a timely fashion. Sounds good to me. The more time you need the better from my standpoint :)

@lovelydinosaur

Copy link
Copy Markdown
Collaborator

Holy smokes

@waylan

waylan commented Jun 8, 2018 •

Copy link
Copy Markdown
Contributor Author

We're getting a test failure in the "integration tests" which do a "strict" build of the MkDocs documentation (the same issue is causing the linkchecker test to fail) . Of course, a "strict" build errors if any warnings are issued. And a build of the MkDocs documentation currently includes the following warning:

WARNING -  Documentation file 'index.md' contains a link to 'img\favicon.ico' 
which does not exist in the documentation directory.

That warning is correct. However, the image does exist in the generated site as it comes from the theme. #872 requests the ability to validate image links in documentation pages and this refactor makes that very easy to implement (it appears that I missed listing that feature and issue in the release notes). The tricky part is when the link points to an internal file which comes from an external source (external links are ignored in this check). Should we include the non-template theme files in the check? A comment I inserted in the code seems to indicate I had intended to do something about this at one point. Perhaps we should even include non-template theme files in the Files collection? The problem is that the Files collection assumes there is only ever one file for any output location. However, it is documented and understood that theme files get written first, then files from the docs_dir can overwrite a file of the same name.

I was thinking I could deal with this later, but didn't consider that it would cause the Integration tests to fail.

@charlesreid1

charlesreid1 commented Jun 11, 2018 •

Copy link
Copy Markdown

Re #1504 and this change:

A README.md file is now treated as an index file within a directory and
will be rendered to index.html (#608).

does this implement the behavior you described in this comment?

when no index.md is found within a dir within the docs_dir, but README.md is found, then README.md is used and treated as if it was index.md

or is the index.md name being done away with entirely in favor of using README.md? ok thanks, take a chill pill

@waylan

waylan commented Jun 12, 2018

Copy link
Copy Markdown
Contributor Author

is the index.md name being done away with entirely in favor of using README.md?

No. I never said that and why would we do that. README.md is an optional alternate index file for those people who want it.

@waylan waylan added this to the 1.0.0 milestone Jun 12, 2018
@hfagerlund

Copy link
Copy Markdown
Contributor

Thanks @waylan. Looking forward to trying this out.

waylan added 4 commits June 22, 2018 11:46
When a byte string and a unicode string are both passed to os.path (join
ect) then returned value is always a byte string. Therefore, we need
every path string to be unicode. This ensures valudation checks that and
if the byte string uses the file system encoding, decodes it. For any
other encoding, a validation error is raised.
@waylan

waylan commented Jun 24, 2018

Copy link
Copy Markdown
Contributor Author

While working on this, I discovered a weird error which I reported in #1516. As the error also exists in master, it appears to not be related to this work, so I'll address that separately.

I almost missed reimplmenting this.
@waylan
waylan merged commit 34ef3ca into mkdocs:master Jun 28, 2018
waylan added a commit to waylan/mkdocs that referenced this pull request Jun 29, 2018
This relimplements mkdocs#1272 on the current code in master. Thanks to 
@funkyfuture for the original work on this.

Also removes some unused code after mkdocs#1504.

Closes mkdocs#1272.
waylan added a commit that referenced this pull request Jun 29, 2018
This relimplements #1272 on the current code in master. Thanks to 
@funkyfuture for the original work on this.

Also removes some unused code after #1504.

Closes #1272.
@waylan

waylan commented Jul 3, 2018

Copy link
Copy Markdown
Contributor Author

FYI to those following this, especially theme authors, the above described behavior for URLs and the use of base_url is being changed slightly due to an oversight on my part. See #1526 for details.

@timani

timani commented Jul 9, 2018

Copy link
Copy Markdown

Thanks for all the updates @waylan, these are all awesome and welcome changes. Is there a way to test a dev build or an ETA of when this will be available as a stable release?

@waylan

waylan commented Jul 9, 2018

Copy link
Copy Markdown
Contributor Author

@timani this will be included in the next release (1.0) when the 1.0 Milestone is complete (we are almost there). In the meantime, you can always install the latest in the master branch directly from GitHub:

pip install https://github.com/mkdocs/mkdocs/archive/master.zip

@timani

timani commented Jul 22, 2018

Copy link
Copy Markdown

great! I'll subscribe to the 1.0 Milestone

I also see 1.0 Beta is already out so that should do the trick

pip install mkdocs==1.0b1

thanks @waylan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants