Skip to content

Commit

Permalink
Add Universal edit button (#2660)
Browse files Browse the repository at this point in the history
* Clean base

* Remove edit btn from a few pages

* Update path logic

* Add hook to mkdocs

* Fix universal edit button functionality and restrict for specific file paths

* Refactor edit button URI handling code into a function

---------

Co-authored-by: Jeroen Beckers <[email protected]>
Co-authored-by: Carlos Holguera <[email protected]>
  • Loading branch information
3 people authored Jul 27, 2024
1 parent b2a3524 commit 1b105b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
43 changes: 43 additions & 0 deletions docs/hooks/edit_button_uri.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging

log = logging.getLogger('mkdocs')

def get_edit_url(src_path, edit_url_mastg, edit_url_masvs):
if src_path.startswith("MASVS"):
edit_url = f"{edit_url_masvs}{src_path}"
edit_url = edit_url.replace("master/MASVS/controls", "master/controls/")
edit_url = edit_url.replace("master/MASVS/", "master/Document/")
elif src_path.startswith("MASTG"):
edit_url = f"{edit_url_mastg}{src_path}"
edit_url = edit_url.replace("master/MASTG/0x", "master/Document/0x")
edit_url = edit_url.replace("master/MASTG/", "master/")
elif src_path.startswith("MASWE"):
edit_url = f"{edit_url_mastg}{src_path}"
edit_url = edit_url.replace("master/MASWE/", "master/weaknesses/")
elif src_path.startswith(("contributing", "donate")):
edit_url = f"{edit_url_mastg}{src_path}"
edit_url = edit_url.replace("master/", "master/docs/")
else:
edit_url = ""

return edit_url

def on_pre_page(page, config, files):
try:
edit_url_mastg = config["editor_url_mastg"]
edit_url_masvs = config["editor_url_masvs"]
except KeyError:
return page

src_path = page.file.src_path

if src_path.startswith(("MASTG", "MASVS", "MASWE", "contributing", "donate")):
edit_url = get_edit_url(src_path, edit_url_mastg, edit_url_masvs)
if edit_url.endswith("/index.md"):
page.edit_url = ""
else:
page.edit_url = edit_url
else:
page.edit_url = ""

return page
5 changes: 4 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ site_name: OWASP Mobile Application Security
repo_url: https://github.com/OWASP/owasp-mastg
repo_name: OWASP/owasp-mastg
# use_directory_urls: false # only set for mkdocs build
edit_uri: "" # disable edit button
editor_url_mastg: "https://github.com/OWASP/owasp-mastg/edit/master/"
editor_url_masvs: "https://github.com/OWASP/owasp-masvs/edit/master/"

nav:
- Home: index.md
Expand Down Expand Up @@ -251,6 +252,7 @@ theme:
- navigation.tracking
- navigation.indexes
- content.code.copy
- content.action.edit

palette:
# - primary: #499FFF
Expand Down Expand Up @@ -321,6 +323,7 @@ hooks:
- docs/hooks/maswe-beta-banner.py
- docs/hooks/add-tags.py
- docs/hooks/add-cross-references.py
- docs/hooks/edit_button_uri.py

extra:
generator: false # removed but recreated in copyright above
Expand Down

0 comments on commit 1b105b4

Please sign in to comment.