Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/generate_redirects_from_source_links
100644 → 100755
Empty file.
16 changes: 15 additions & 1 deletion hypernode/common/docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os.path
from pathlib import Path
from typing import List
from typing import List, Optional

import mdformat
import yaml

from hypernode.common.settings import DOCS_DIR

Expand All @@ -15,3 +18,14 @@ def get_all_docs() -> List[Path]:
for file in markdown_files:
result.append(Path(root) / file)
return result


def write_doc(path: Path, contents: str, frontmatter: Optional[dict]) -> None:
if frontmatter:
fm_yaml = yaml.dump(frontmatter, default_flow_style=False)
contents = "---\n" + fm_yaml + "---\n\n" + contents

contents = mdformat.text(contents, extensions=["frontmatter", "myst"])

with open(path, mode="w", encoding="utf-8") as f:
f.write(contents)
8 changes: 2 additions & 6 deletions hypernode/redirect/generate_redirects_from_source_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from typing import Optional
from urllib.parse import urlparse

import yaml
from frontmatter import Frontmatter

from hypernode.common.docs import get_all_docs
from hypernode.common.docs import get_all_docs, write_doc

SOURCE_PATTERN = re.compile(r"^<!-- source: (.+) -->$")

Expand All @@ -25,16 +24,13 @@ def set_source_path_redirect(doc: Path, source_path: str) -> None:
fm = Frontmatter.read_file(doc)
attributes = fm["attributes"] or {}
attributes["redirect_from"] = [source_path]
fm_yaml = yaml.dump(attributes, default_flow_style=False)

body = fm["body"]
if not body:
with open(doc, mode="r", encoding="utf-8") as f:
body = f.read()

with open(doc, mode="w", encoding="utf-8") as f:
contents = "---\n" + fm_yaml + "---\n\n" + body
f.write(contents)
write_doc(doc, body, attributes)


def main():
Expand Down