-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(markup): simplify link rendering logic
- Streamlined the conditional checks for various link types, consolidating the logic to improve readability and maintainability. Added support for raw URLs and enhanced handling of YouTube links. Closes issue #8
- Loading branch information
Showing
1 changed file
with
51 additions
and
45 deletions.
There are no files selected for viewing
96 changes: 51 additions & 45 deletions
96
themes/aurelia-theme/layouts/_default/_markup/render-link.html
This file contains 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 |
---|---|---|
@@ -1,52 +1,58 @@ | ||
{{ $url := .Destination }} | ||
{{ $isYoutube := findRE "(youtube:|youtu.be/|youtube.com/watch)" $url 1 }} | ||
{{ $isGist := findRE "^(gist:|https://gist.github.com/)" $url 1 }} | ||
{{ $isStackBlitz := findRE "stackblitz.com/edit/([^/?]+)" $url 1 }} | ||
{{ $isCodePen := findRE "codepen.io/([^/]+)/pen/([^/?]+)" $url 1 }} | ||
{{ $isNpmPackage := findRE "npmjs.com/package/([^/?]+)" $url 1 }} | ||
{{ $isGitHubSponsors := findRE "github.com/sponsors/" $url 1 }} | ||
{{ $isGitHubRepo := and (not $isGitHubSponsors) (findRE "github.com/([^/]+/[^/]+)(?:/|$)" $url 1) }} | ||
{{ $isDumberGist := findRE "gist.dumber.app/\\?gist=([^/?&]+)" $url 1 }} | ||
{{ $isMarkdownLink := .Text }} {{/* Will be non-empty for proper markdown links */}} | ||
{{ $isRawUrl := not $isMarkdownLink }} | ||
|
||
{{ if $isYoutube }} | ||
{{ $videoID := "" }} | ||
{{ if findRE "youtube.com/watch" $url 1 }} | ||
{{ $videoID = (index (findRE "[?&]v=([A-Za-z0-9_-]{11})" $url 1) 0) }} | ||
{{ $videoID = replaceRE "[?&]v=" "" $videoID }} | ||
<!-- YouTube.com Video ID: {{ $videoID }} --> | ||
{{ else if findRE "youtu.be/" $url 1 }} | ||
{{ $videoID = (index (findRE "youtu.be/([A-Za-z0-9_-]{11})" $url 1) 0) }} | ||
{{ $videoID = replaceRE "youtu.be/" "" $videoID }} | ||
<!-- YoutuBe Video ID: {{ $videoID }} --> | ||
{{ if $isRawUrl }} | ||
{{ $isYoutube := findRE "(youtube:|youtu.be/|youtube.com/watch)" $url 1 }} | ||
{{ $isGist := findRE "^(gist:|https://gist.github.com/)" $url 1 }} | ||
{{ $isStackBlitz := findRE "stackblitz.com/edit/([^/?]+)" $url 1 }} | ||
{{ $isCodePen := findRE "codepen.io/([^/]+)/pen/([^/?]+)" $url 1 }} | ||
{{ $isNpmPackage := findRE "npmjs.com/package/([^/?]+)" $url 1 }} | ||
{{ $isGitHubSponsors := findRE "github.com/sponsors/" $url 1 }} | ||
{{ $isGitHubCommit := findRE "github.com/[^/]+/[^/]+/commit/" $url 1 }} | ||
{{ $isGitHubFile := findRE "github.com/[^/]+/[^/]+/blob/" $url 1 }} | ||
{{ $isGitHubRepo := and (not $isGitHubSponsors) (not $isGitHubCommit) (not $isGitHubFile) (findRE "github.com/([^/]+/[^/]+)(?:/|$)" $url 1) }} | ||
{{ $isDumberGist := findRE "gist.dumber.app/\\?gist=([^/?&]+)" $url 1 }} | ||
|
||
{{ if $isYoutube }} | ||
{{ $videoID := "" }} | ||
{{ if findRE "youtube.com/watch" $url 1 }} | ||
{{ $videoID = (index (findRE "[?&]v=([A-Za-z0-9_-]{11})" $url 1) 0) }} | ||
{{ $videoID = replaceRE "[?&]v=" "" $videoID }} | ||
{{ else if findRE "youtu.be/" $url 1 }} | ||
{{ $videoID = (index (findRE "youtu.be/([A-Za-z0-9_-]{11})" $url 1) 0) }} | ||
{{ $videoID = replaceRE "youtu.be/" "" $videoID }} | ||
{{ else }} | ||
{{ $videoID = replaceRE "youtube:" "" $url }} | ||
{{ end }} | ||
{{ partial "render-hooks/youtube.html" (dict "Destination" $videoID) }} | ||
{{ else if $isGist }} | ||
{{ $gistID := replaceRE "^(gist:|https://gist.github.com/)" "" $url }} | ||
{{ partial "render-hooks/gist.html" (dict "Destination" $gistID) }} | ||
{{ else if $isStackBlitz }} | ||
{{ $projectId := index (findRE "stackblitz.com/edit/([^/?]+)" $url 1) 0 }} | ||
{{ $projectId = replaceRE "stackblitz.com/edit/" "" $projectId }} | ||
{{ partial "render-hooks/stackblitz.html" (dict "Destination" $projectId "Height" "800px") }} | ||
{{ else if $isCodePen }} | ||
{{ $matches := findRE "codepen.io/([^/]+)/pen/([^/?]+)" $url 1 }} | ||
{{ $user := replaceRE "codepen.io/([^/]+)/pen/.*" "$1" (index $matches 0) }} | ||
{{ $penId := replaceRE "codepen.io/[^/]+/pen/([^/?]+)" "$1" (index $matches 0) }} | ||
{{ partial "render-hooks/codepen.html" (dict "User" $user "PenId" $penId "Height" "400") }} | ||
{{ else if $isNpmPackage }} | ||
{{ $packageName := index (findRE "npmjs.com/package/([^/?]+)" $url 1) 0 }} | ||
{{ $packageName = replaceRE "npmjs.com/package/" "" $packageName }} | ||
{{ partial "render-hooks/npm-package.html" (dict "Destination" $packageName) }} | ||
{{ else if $isGitHubRepo }} | ||
{{ $repoPath := index (findRE "github.com/([^/]+/[^/]+)(?:/|$)" $url 1) 0 }} | ||
{{ $repoPath = replaceRE "github.com/" "" $repoPath }} | ||
{{ partial "render-hooks/github-repo.html" (dict "Destination" $repoPath) }} | ||
{{ else if $isDumberGist }} | ||
{{ $gistId := index (findRE "gist=([^/?&]+)" $url 1) 0 }} | ||
{{ $gistId = replaceRE "gist=" "" $gistId }} | ||
{{ partial "render-hooks/dumber-gist.html" (dict "Destination" $gistId "Height" "400px") }} | ||
{{ else }} | ||
{{ $videoID = replaceRE "youtube:" "" $url }} | ||
<!-- Direct Video ID: {{ $videoID }} --> | ||
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a> | ||
{{ end }} | ||
{{ partial "render-hooks/youtube.html" (dict "Destination" $videoID) }} | ||
{{ else if $isGist }} | ||
{{ $gistID := replaceRE "^(gist:|https://gist.github.com/)" "" $url }} | ||
{{ partial "render-hooks/gist.html" (dict "Destination" $gistID) }} | ||
{{ else if $isStackBlitz }} | ||
{{ $projectId := index (findRE "stackblitz.com/edit/([^/?]+)" $url 1) 0 }} | ||
{{ $projectId = replaceRE "stackblitz.com/edit/" "" $projectId }} | ||
{{ partial "render-hooks/stackblitz.html" (dict "Destination" $projectId "Height" "800px") }} | ||
{{ else if $isCodePen }} | ||
{{ $matches := findRE "codepen.io/([^/]+)/pen/([^/?]+)" $url 1 }} | ||
{{ $user := replaceRE "codepen.io/([^/]+)/pen/.*" "$1" (index $matches 0) }} | ||
{{ $penId := replaceRE "codepen.io/[^/]+/pen/([^/?]+)" "$1" (index $matches 0) }} | ||
{{ partial "render-hooks/codepen.html" (dict "User" $user "PenId" $penId "Height" "400") }} | ||
{{ else if $isNpmPackage }} | ||
{{ $packageName := index (findRE "npmjs.com/package/([^/?]+)" $url 1) 0 }} | ||
{{ $packageName = replaceRE "npmjs.com/package/" "" $packageName }} | ||
{{ partial "render-hooks/npm-package.html" (dict "Destination" $packageName) }} | ||
{{ else if $isGitHubRepo }} | ||
{{ $repoPath := index (findRE "github.com/([^/]+/[^/]+)(?:/|$)" $url 1) 0 }} | ||
{{ $repoPath = replaceRE "github.com/" "" $repoPath }} | ||
{{ partial "render-hooks/github-repo.html" (dict "Destination" $repoPath) }} | ||
{{ else if $isDumberGist }} | ||
{{ $gistId := index (findRE "gist=([^/?&]+)" $url 1) 0 }} | ||
{{ $gistId = replaceRE "gist=" "" $gistId }} | ||
{{ partial "render-hooks/dumber-gist.html" (dict "Destination" $gistId "Height" "400px") }} | ||
{{ else }} | ||
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a> | ||
{{ end }} |