Skip to content

Commit

Permalink
correct routing doc files
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Sep 7, 2023
1 parent efdf35d commit bfcaa45
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/doc_builder/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
from pathlib import Path

from doc_builder import build_doc, update_versions_file
from doc_builder.utils import get_default_branch_name, get_doc_config, locate_kit_folder, read_doc_config
from doc_builder.utils import (
get_default_branch_name,
get_doc_config,
locate_kit_folder,
read_doc_config,
sveltify_file_route,
)


def check_node_is_available():
Expand Down Expand Up @@ -121,15 +127,23 @@ def build_command(args):
shutil.copytree(kit_folder, tmp_dir / "kit")
# Manual copy and overwrite from output_path to tmp_dir / "kit" / "src" / "routes"
# We don't use shutil.copytree as tmp_dir / "kit" / "src" / "routes" exists and contains important files.
svelte_kit_routes_dir = tmp_dir / "kit" / "src" / "routes"
for f in output_path.iterdir():
dest = tmp_dir / "kit" / "src" / "routes" / f.name
dest = svelte_kit_routes_dir / f.name
if f.is_dir():
# Remove the dest folder if it exists
if dest.is_dir():
shutil.rmtree(dest)
shutil.copytree(f, dest)
else:
shutil.copy(f, dest)
# make mdx file paths comply with the sveltekit 1.0 routing mechanism
# see more: https://learn.svelte.dev/tutorial/pages
for mdx_file_path in svelte_kit_routes_dir.rglob("*.mdx"):
new_path = sveltify_file_route(mdx_file_path)
parent_path = os.path.dirname(new_path)
os.makedirs(parent_path, exist_ok=True)
shutil.move(mdx_file_path, new_path)

# Move the objects.inv file at the root
if not args.not_python_module:
Expand Down
13 changes: 13 additions & 0 deletions src/doc_builder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,16 @@ def get_cached_repo():
cwd=cache_repo_path,
)
return cache_repo_path


def sveltify_file_route(filename):
"""
Given `filename` /path/abc/xyz.mdx, return /path/abc/xyz/+page.svelte
"""
# filename can be PosixPath or str
filename = str(filename)
# Check if the filename ends with '.svelte'
if filename.endswith(".mdx"):
# Replace the '{name}.mdx' with '{name}/+page.svelte'
return filename.rsplit(".", 1)[0] + "/+page.svelte"
return filename
18 changes: 17 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathlib import Path

import yaml
from doc_builder.utils import update_versions_file
from doc_builder.utils import sveltify_file_route, update_versions_file


class UtilsTester(unittest.TestCase):
Expand Down Expand Up @@ -66,3 +66,19 @@ def test_update_versions_file(self):
yml_str = tmp_yml.read()
expected_yml = "- version: main\n- version: v4.2.2\n"
self.assertEqual(yml_str, expected_yml)

def test_sveltify_file_route(self):
mdx_file_path = "guide.mdx"
svelte_file_path = sveltify_file_route(mdx_file_path)
expected_path = "guide/+page.svelte"
self.assertEqual(svelte_file_path, expected_path)

mdx_file_path = "xyz/abc/guide.mdx"
svelte_file_path = sveltify_file_route(mdx_file_path)
expected_path = "xyz/abc/guide/+page.svelte"
self.assertEqual(svelte_file_path, expected_path)

mdx_file_path = "/xyz/abc/guide.mdx"
svelte_file_path = sveltify_file_route(mdx_file_path)
expected_path = "/xyz/abc/guide/+page.svelte"
self.assertEqual(svelte_file_path, expected_path)

0 comments on commit bfcaa45

Please sign in to comment.