-
Notifications
You must be signed in to change notification settings - Fork 74
ENT-14112: Converted bare URLs into clickable links #3677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aleksandrychev
wants to merge
1
commit into
cfengine:master
Choose a base branch
from
aleksandrychev:ENT-14112
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import re | ||
|
|
||
| IPV4_URL_RE = re.compile( | ||
| r""" | ||
| (?<![<(`/\w"']) # skip if already inside <autolink>, [text](link), | ||
| # `code`, an href="..."/'...' attribute, or a longer | ||
| # token (word char / slash) that we'd be splitting | ||
| ( # capture group 1: the URL itself | ||
| https?:// # scheme | ||
| (?:\d{1,3}\.){3}\d{1,3} # IPv4 | ||
| (?::\d+)? # optional :port | ||
| (?:/[^\s<>)\]`'"]*)? # optional /path — stop at whitespace or chars | ||
| # that typically close/quote the URL | ||
| ) | ||
| """, | ||
| re.VERBOSE, | ||
| ) | ||
|
|
||
| FENCE_RE = re.compile( | ||
| r""" | ||
| ^(\s*) # leading indentation (captured but unused) | ||
| (```+|~~~+) # fence marker: 3+ backticks or 3+ tildes | ||
| """, | ||
| re.VERBOSE, | ||
| ) | ||
|
|
||
|
|
||
| def run(config): | ||
| for file in config["markdown_files"]: | ||
| process(file) | ||
|
|
||
|
|
||
| def process(file_path): | ||
| try: | ||
| with open(file_path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| transformed = transform(content) | ||
|
|
||
| if transformed != content: | ||
| with open(file_path, "w", encoding="utf-8") as f: | ||
| f.write(transformed) | ||
| except Exception as e: | ||
| print(f"cfdoc_ip_autolink: error processing {file_path}: {e}") | ||
| raise | ||
|
|
||
|
|
||
| def transform(content): | ||
| out_lines = [] | ||
| in_fence = False | ||
| fence_marker = None | ||
|
|
||
| for line in content.splitlines(keepends=True): | ||
| if in_fence: | ||
| out_lines.append(line) | ||
| if fence_marker and line.lstrip().startswith(fence_marker): | ||
| in_fence = False | ||
| fence_marker = None | ||
| continue | ||
|
|
||
| m = FENCE_RE.match(line) | ||
| if m: | ||
| fence_marker = m.group(2) | ||
| in_fence = True | ||
| out_lines.append(line) | ||
| continue | ||
|
|
||
| out_lines.append(transform_line(line)) | ||
|
|
||
| return "".join(out_lines) | ||
|
|
||
|
|
||
| # Stripped from the end of a URL so sentence punctuation stays in the prose. | ||
| TRAILING_PUNCT = ".,;:!?" | ||
|
|
||
|
|
||
| def _wrap(match): | ||
| url = match.group(1) | ||
| trailing = "" | ||
| while url and url[-1] in TRAILING_PUNCT: | ||
| trailing = url[-1] + trailing | ||
| url = url[:-1] | ||
| return f"<{url}>{trailing}" | ||
|
|
||
|
|
||
| def transform_line(line): | ||
| # Split on inline backtick spans so URLs inside `code` are untouched. | ||
| parts = re.split(r"(`+[^`\n]*`+)", line) | ||
| for i, chunk in enumerate(parts): | ||
| if i % 2 == 1: | ||
| continue | ||
| parts[i] = IPV4_URL_RE.sub(_wrap, chunk) | ||
| return "".join(parts) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,11 @@ wrapperClass = 'hlc' | |
| [markup.goldmark.renderer] | ||
| unsafe = true # Allow HTML in md files | ||
|
|
||
| # Automatically convert bare URLs into clickable links | ||
| # linkify skips IP addresses, which are handled by pre-process scripts | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think you should add the name of that specific script |
||
| [markup.goldmark.extensions] | ||
| linkify = true | ||
|
|
||
| [params.sitemap] | ||
| baseUrl = "https://docs.cfengine.com/docs/%branch%" | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think you should add a docstring explaining what this script does, along with any important details. For example does it look for URLs anywhere? Only when they are on their own line? Inside code blocks?