Skip to content

Commit 42ff3a1

Browse files
committed
Support docs from the root directory.
1 parent ae93314 commit 42ff3a1

File tree

8 files changed

+43
-77
lines changed

8 files changed

+43
-77
lines changed

mkdocs/config/config_options.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -734,14 +734,6 @@ def post_validation(self, config: Config, key_name: str):
734734
if not config.config_file_path:
735735
return
736736

737-
# Validate that the dir is not the parent dir of the config file.
738-
if os.path.dirname(config.config_file_path) == config[key_name]:
739-
raise ValidationError(
740-
f"The '{key_name}' should not be the parent directory of the"
741-
f" config file. Use a child directory instead so that the"
742-
f" '{key_name}' is a sibling of the config file."
743-
)
744-
745737

746738
class File(FilesystemObject):
747739
"""
@@ -800,13 +792,6 @@ def post_validation(self, config: Config, key_name: str):
800792
f"it will be deleted if --clean is passed to mkdocs build. "
801793
f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')"
802794
)
803-
elif (site_dir + os.sep).startswith(docs_dir.rstrip(os.sep) + os.sep):
804-
raise ValidationError(
805-
f"The 'site_dir' should not be within the 'docs_dir' as this "
806-
f"leads to the build directory being copied into itself and "
807-
f"duplicate nested files in the 'site_dir'. "
808-
f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')"
809-
)
810795

811796

812797
class Theme(BaseConfigOption[theme.Theme]):

mkdocs/structure/files.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ def get_files(config: MkDocsConfig) -> Files:
343343
files: list[File] = []
344344
conflicting_files: list[tuple[File, File]] = []
345345
for source_dir, dirnames, filenames in os.walk(config['docs_dir'], followlinks=True):
346+
# Skip processing any files in the site_dir
347+
if os.path.abspath(source_dir) == os.path.abspath(config['site_dir']):
348+
log.info(f"Excluding files in site_dir '{config['site_dir']}'.")
349+
continue
350+
346351
relative_dir = os.path.relpath(source_dir, config['docs_dir'])
347352
dirnames.sort()
348353
filenames.sort(key=_file_sort_key)

mkdocs/tests/config/config_options_legacy_tests.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,6 @@ class Schema:
768768
)
769769
self.assertEqual(conf['dir'], os.path.join(base_path, 'foo'))
770770

771-
def test_site_dir_is_config_dir_fails(self):
772-
class Schema:
773-
dir = c.DocsDir()
774-
775-
with self.expect_error(
776-
dir="The 'dir' should not be the parent directory of the config file. "
777-
"Use a child directory instead so that the 'dir' is a sibling of the config file."
778-
):
779-
self.get_config(
780-
Schema,
781-
{'dir': '.'},
782-
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'),
783-
)
784-
785771

786772
class ListOfPathsTest(TestCase):
787773
def test_valid_path(self):
@@ -882,23 +868,6 @@ def test_doc_dir_in_site_dir(self):
882868
):
883869
self.get_config(self.Schema, test_config)
884870

885-
def test_site_dir_in_docs_dir(self):
886-
j = os.path.join
887-
888-
test_configs = (
889-
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')},
890-
{'docs_dir': '.', 'site_dir': 'site'},
891-
{'docs_dir': '', 'site_dir': 'site'},
892-
{'docs_dir': '/', 'site_dir': 'site'},
893-
)
894-
895-
for test_config in test_configs:
896-
with self.subTest(test_config):
897-
with self.expect_error(
898-
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*")
899-
):
900-
self.get_config(self.Schema, test_config)
901-
902871
def test_common_prefix(self):
903872
"""Legitimate settings with common prefixes should not fail validation."""
904873
test_configs = (

mkdocs/tests/config/config_options_tests.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -972,20 +972,6 @@ class Schema(Config):
972972
)
973973
self.assertEqual(conf.dir, os.path.join(base_path, 'foo'))
974974

975-
def test_site_dir_is_config_dir_fails(self) -> None:
976-
class Schema(Config):
977-
dir = c.DocsDir()
978-
979-
with self.expect_error(
980-
dir="The 'dir' should not be the parent directory of the config file. "
981-
"Use a child directory instead so that the 'dir' is a sibling of the config file."
982-
):
983-
self.get_config(
984-
Schema,
985-
{'dir': '.'},
986-
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'),
987-
)
988-
989975

990976
class ListOfPathsTest(TestCase):
991977
def test_valid_path(self) -> None:
@@ -1095,23 +1081,6 @@ def test_doc_dir_in_site_dir(self) -> None:
10951081
):
10961082
self.get_config(self.Schema, test_config)
10971083

1098-
def test_site_dir_in_docs_dir(self) -> None:
1099-
j = os.path.join
1100-
1101-
test_configs = (
1102-
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')},
1103-
{'docs_dir': '.', 'site_dir': 'site'},
1104-
{'docs_dir': '', 'site_dir': 'site'},
1105-
{'docs_dir': '/', 'site_dir': 'site'},
1106-
)
1107-
1108-
for test_config in test_configs:
1109-
with self.subTest(test_config):
1110-
with self.expect_error(
1111-
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*")
1112-
):
1113-
self.get_config(self.Schema, test_config)
1114-
11151084
def test_common_prefix(self) -> None:
11161085
"""Legitimate settings with common prefixes should not fail validation."""
11171086
test_configs = (
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Welcome to MkDocs
2+
3+
For full documentation visit [mkdocs.org](https://www.mkdocs.org).
4+
5+
## Commands
6+
7+
* `mkdocs new [dir-name]` - Create a new project.
8+
* `mkdocs serve` - Start the live-reloading docs server.
9+
* `mkdocs build` - Build the documentation site.
10+
* `mkdocs help` - Print this help message.
11+
12+
## Project layout
13+
14+
mkdocs.yml # The configuration file.
15+
docs/
16+
index.md # The documentation homepage.
17+
... # Other markdown pages, images and other files.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: MyTest
2+
docs_dir: .
3+
4+
nav:
5+
- 'README.md'
6+
- 'testing.md'
7+
8+
site_author: "Allison Thackston"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Page content.

mkdocs/tests/structure/file_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,18 @@ def test_get_files_exclude_readme_with_index(self, tdir):
672672
self.assertIsInstance(files, Files)
673673
self.assertEqual([f.src_uri for f in files], ['index.md', 'foo.md'])
674674

675+
@tempdir(
676+
files=[
677+
'index.md',
678+
'site/foo.md',
679+
]
680+
)
681+
def test_get_files_exclude_site_dir(self, tdir):
682+
config = load_config(docs_dir=tdir, site_dir=os.path.join(tdir, "site"))
683+
files = get_files(config)
684+
self.assertIsInstance(files, Files)
685+
self.assertEqual([f.src_uri for f in files], ['index.md'])
686+
675687
@tempdir()
676688
@tempdir(files={'test.txt': 'source content'})
677689
def test_copy_file(self, src_dir, dest_dir):

0 commit comments

Comments
 (0)