Build site navigation from config and files.
(files: Files, config: MkDocsConfig)
| 128 | |
| 129 | |
| 130 | def get_navigation(files: Files, config: MkDocsConfig) -> Navigation: |
| 131 | """Build site navigation from config and files.""" |
| 132 | documentation_pages = files.documentation_pages() |
| 133 | nav_config = config['nav'] |
| 134 | if nav_config is None: |
| 135 | documentation_pages = sorted(documentation_pages, key=file_sort_key) |
| 136 | nav_config = nest_paths(f.src_uri for f in documentation_pages if f.inclusion.is_in_nav()) |
| 137 | items = _data_to_navigation(nav_config, files, config) |
| 138 | if not isinstance(items, list): |
| 139 | items = [items] |
| 140 | |
| 141 | # Get only the pages from the navigation, ignoring any sections and links. |
| 142 | pages = _get_by_type(items, Page) |
| 143 | |
| 144 | # Include next, previous and parent links. |
| 145 | _add_previous_and_next_links(pages) |
| 146 | _add_parent_links(items) |
| 147 | |
| 148 | missing_from_config = [] |
| 149 | for file in documentation_pages: |
| 150 | if file.page is None: |
| 151 | # Any documentation files not found in the nav should still have an associated page, so we |
| 152 | # create them here. The Page object will automatically be assigned to `file.page` during |
| 153 | # its creation (and this is the only way in which these page objects are accessible). |
| 154 | Page(None, file, config) |
| 155 | if file.inclusion.is_in_nav(): |
| 156 | missing_from_config.append(file.src_path) |
| 157 | if missing_from_config: |
| 158 | log.log( |
| 159 | config.validation.nav.omitted_files, |
| 160 | 'The following pages exist in the docs directory, but are not ' |
| 161 | 'included in the "nav" configuration:\n - ' + '\n - '.join(missing_from_config), |
| 162 | ) |
| 163 | |
| 164 | links = _get_by_type(items, Link) |
| 165 | for link in links: |
| 166 | scheme, netloc, path, query, fragment = urlsplit(link.url) |
| 167 | if scheme or netloc: |
| 168 | log.debug(f"An external link to '{link.url}' is included in the 'nav' configuration.") |
| 169 | elif ( |
| 170 | link.url.startswith('/') |
| 171 | and config.validation.nav.absolute_links |
| 172 | is not _AbsoluteLinksValidationValue.RELATIVE_TO_DOCS |
| 173 | ): |
| 174 | log.log( |
| 175 | config.validation.nav.absolute_links, |
| 176 | f"An absolute path to '{link.url}' is included in the 'nav' " |
| 177 | "configuration, which presumably points to an external resource.", |
| 178 | ) |
| 179 | else: |
| 180 | log.log( |
| 181 | config.validation.nav.not_found, |
| 182 | f"A reference to '{link.url}' is included in the 'nav' " |
| 183 | "configuration, which is not found in the documentation files.", |
| 184 | ) |
| 185 | return Navigation(items, pages) |
| 186 | |
| 187 |
searching dependent graphs…