Skip to content

Commit ec4b658

Browse files
authored
[Feature] add colab link to automaitc nb -> md conversion (#473)
1 parent 1b2e3b3 commit ec4b658

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.github/workflows/build_main_documentation.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,16 @@ jobs:
154154
155155
- name: Convert notebooks to markdown files
156156
if: inputs.convert_notebooks
157-
run: doc-builder notebook-to-mdx ${{ env.doc_folder }}
157+
run: |
158+
cd ${{ env.doc_folder }}
159+
remote_url=$(git remote get-url origin)
160+
trimmed_url=${remote_url#https://}
161+
trimmed_url=${trimmed_url#github.com/}
162+
trimmed_url=${trimmed_url%.git}
163+
current_branch=$(git branch --show-current)
164+
cd ..
165+
166+
doc-builder notebook-to-mdx ${{ env.doc_folder }} --open_notebook_prefix https://colab.research.google.com/github/$trimmed_url/blob/$current_branch
158167
159168
- name: Make documentation
160169
shell: bash

.github/workflows/build_pr_documentation.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,16 @@ jobs:
140140
141141
- name: Convert notebooks to markdown files
142142
if: inputs.convert_notebooks
143-
run: doc-builder notebook-to-mdx ${{ env.doc_folder }}
143+
run: |
144+
cd ${{ env.doc_folder }}
145+
remote_url=$(git remote get-url origin)
146+
trimmed_url=${remote_url#https://}
147+
trimmed_url=${trimmed_url#github.com/}
148+
trimmed_url=${trimmed_url%.git}
149+
current_branch=$(git branch --show-current)
150+
cd ..
151+
152+
doc-builder notebook-to-mdx ${{ env.doc_folder }} --open_notebook_prefix https://colab.research.google.com/github/$trimmed_url/blob/$current_branch
144153
145154
- name: Make documentation
146155
env:

src/doc_builder/commands/notebook_to_mdx.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def notebook_to_mdx_command(args):
6565
mdx_file_name = notebook_path.name[: -len(".ipynb")] + ".md"
6666
output_dir = notebook_path.parent if args.output_dir is None else Path(args.output_dir).resolve()
6767
dest_file_path = output_dir / mdx_file_name
68+
69+
if src_path.is_dir() and args.open_notebook_prefix is not None:
70+
relative_path = notebook_path.relative_to(src_path)
71+
colab_link = f"{args.open_notebook_prefix}/{str(relative_path)}"
72+
colab_link_component = f'<DocNotebookDropdown classNames="absolute z-10 right-0 top-0" options={{[{{label: "Google Colab", value: "{colab_link}"}}]}} />/>'
73+
mdx_content = f"{colab_link_component}\n\n" + mdx_content
74+
6875
with open(dest_file_path, "w", encoding="utf-8") as f:
6976
f.write(mdx_content)
7077

@@ -88,6 +95,12 @@ def notebook_to_mdx_command_parser(subparsers=None):
8895
default=119,
8996
help="The number of maximum characters per line.",
9097
)
98+
parser.add_argument(
99+
"--open_notebook_prefix",
100+
type=str,
101+
default=None,
102+
help="Example: https://colab.research.google.com/github/{user}/{repo}/blob/{branch}",
103+
)
91104

92105
if subparsers is not None:
93106
parser.set_defaults(func=notebook_to_mdx_command)

0 commit comments

Comments
 (0)