Repository: mkdocs/mkdocs

Issue:
Unexpected behaviour with page.is_homepage
Starting a new site and rolling my own theme. Came across some slightly odd behaviour.

Mkdocs version 1.0.4
Python version 3.7.1

**Expected:**
`page.is_homepage` evaluates to True on the home (index.md) of the site, and false on all other pages.

**Actual:**
`page.is_homepage` evaluates to True on the home (index.md), and on any other index.md that is included in the nav object without nesting.

**Examples:**

The unexpected result:

```
nav:
  - Home: index.md  <--- page.is_homepage evaluates to True
  - About: about.md <--- page.is_homepage evaluates to False
  - Projects: projects/index.md <--- page.is_homepage evaluates to True
```

Changing the filename causes it to evaluate to false:

```
nav:
  - Home: index.md  <--- page.is_homepage evaluates to True
  - About: about.md <--- page.is_homepage evaluates to False
  - Projects: projects/test.md <--- page.is_homepage evaluates to False
```

If I tweak it a bit, so that the sections are nested, then it evaluates to false as I'd expect:

```
nav:
  - About: 
      - About: about.md <--- page.is_homepage evaluates to False
  - Projects: 
       - Project home: projects/index.md <--- page.is_homepage evaluates to False
```

This feels like a bug - especially as simply changing the markdown file name causes the behaviour to change.




Discussion:
Yes, that's a bug. Thanks for the report.

https://github.com/mkdocs/mkdocs/blob/98823e09b9cf35e6e871f356b448fb9a48eef05d/mkdocs/structure/pages.py#L94-L96

We are only checking that the page is an index page at the root of the `nav` and not looking at the path at all. However, we do not require the `nav` to match the file structure. We simply need to add a check that the path is at the `doc_dir` root. Perhaps `and '/' not in self.url`.

Resolving patch:
diff --git a/mkdocs/structure/pages.py b/mkdocs/structure/pages.py
--- a/mkdocs/structure/pages.py
+++ b/mkdocs/structure/pages.py
@@ -93,7 +93,7 @@ def is_top_level(self):
 
     @property
     def is_homepage(self):
-        return self.is_top_level and self.is_index
+        return self.is_top_level and self.is_index and self.file.url == '.'
 
     @property
     def url(self):


Test patch:
diff --git a/mkdocs/tests/structure/page_tests.py b/mkdocs/tests/structure/page_tests.py
--- a/mkdocs/tests/structure/page_tests.py
+++ b/mkdocs/tests/structure/page_tests.py
@@ -70,6 +70,54 @@ def test_nested_index_page(self):
         self.assertEqual(pg.title, 'Foo')
         self.assertEqual(pg.toc, [])
 
+    def test_nested_index_page_no_parent(self):
+        cfg = load_config(docs_dir=self.DOCS_DIR)
+        fl = File('sub1/index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
+        pg = Page('Foo', fl, cfg)
+        pg.parent = None  # non-homepage at nav root level; see #1919.
+        self.assertEqual(pg.url, 'sub1/')
+        self.assertEqual(pg.abs_url, None)
+        self.assertEqual(pg.canonical_url, None)
+        self.assertEqual(pg.edit_url, None)
+        self.assertEqual(pg.file, fl)
+        self.assertEqual(pg.content, None)
+        self.assertFalse(pg.is_homepage)
+        self.assertTrue(pg.is_index)
+        self.assertTrue(pg.is_page)
+        self.assertFalse(pg.is_section)
+        self.assertTrue(pg.is_top_level)
+        self.assertEqual(pg.markdown, None)
+        self.assertEqual(pg.meta, {})
+        self.assertEqual(pg.next_page, None)
+        self.assertEqual(pg.parent, None)
+        self.assertEqual(pg.previous_page, None)
+        self.assertEqual(pg.title, 'Foo')
+        self.assertEqual(pg.toc, [])
+
+    def test_nested_index_page_no_parent_no_directory_urls(self):
+        cfg = load_config(docs_dir=self.DOCS_DIR, use_directory_urls=False)
+        fl = File('sub1/index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
+        pg = Page('Foo', fl, cfg)
+        pg.parent = None  # non-homepage at nav root level; see #1919.
+        self.assertEqual(pg.url, 'sub1/index.html')
+        self.assertEqual(pg.abs_url, None)
+        self.assertEqual(pg.canonical_url, None)
+        self.assertEqual(pg.edit_url, None)
+        self.assertEqual(pg.file, fl)
+        self.assertEqual(pg.content, None)
+        self.assertFalse(pg.is_homepage)
+        self.assertTrue(pg.is_index)
+        self.assertTrue(pg.is_page)
+        self.assertFalse(pg.is_section)
+        self.assertTrue(pg.is_top_level)
+        self.assertEqual(pg.markdown, None)
+        self.assertEqual(pg.meta, {})
+        self.assertEqual(pg.next_page, None)
+        self.assertEqual(pg.parent, None)
+        self.assertEqual(pg.previous_page, None)
+        self.assertEqual(pg.title, 'Foo')
+        self.assertEqual(pg.toc, [])
+
     def test_nested_nonindex_page(self):
         cfg = load_config(docs_dir=self.DOCS_DIR)
         fl = File('sub1/non-index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])