Add option to open external links in new tab
This commit is contained in:
		
							parent
							
								
									ac70b2f668
								
							
						
					
					
						commit
						606ed76189
					
				
							
								
								
									
										1
									
								
								Gemfile
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								Gemfile
									
									
									
									
									
								
							| @ -7,3 +7,4 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |||||||
| gem "jekyll", "~> 4.0" | gem "jekyll", "~> 4.0" | ||||||
| gem "jekyll-last-modified-at", git: "https://github.com/maximevaillancourt/jekyll-last-modified-at", branch: "add-support-for-files-in-git-submodules" | gem "jekyll-last-modified-at", git: "https://github.com/maximevaillancourt/jekyll-last-modified-at", branch: "add-support-for-files-in-git-submodules" | ||||||
| gem "webrick", "~> 1.7" | gem "webrick", "~> 1.7" | ||||||
|  | gem "nokogiri" | ||||||
|  | |||||||
| @ -51,10 +51,15 @@ GEM | |||||||
|       rb-fsevent (~> 0.10, >= 0.10.3) |       rb-fsevent (~> 0.10, >= 0.10.3) | ||||||
|       rb-inotify (~> 0.9, >= 0.9.10) |       rb-inotify (~> 0.9, >= 0.9.10) | ||||||
|     mercenary (0.4.0) |     mercenary (0.4.0) | ||||||
|  |     mini_portile2 (2.5.0) | ||||||
|  |     nokogiri (1.11.1) | ||||||
|  |       mini_portile2 (~> 2.5.0) | ||||||
|  |       racc (~> 1.4) | ||||||
|     pathutil (0.16.2) |     pathutil (0.16.2) | ||||||
|       forwardable-extended (~> 2.6) |       forwardable-extended (~> 2.6) | ||||||
|     posix-spawn (0.3.15) |     posix-spawn (0.3.15) | ||||||
|     public_suffix (4.0.6) |     public_suffix (4.0.6) | ||||||
|  |     racc (1.5.2) | ||||||
|     rb-fsevent (0.10.4) |     rb-fsevent (0.10.4) | ||||||
|     rb-inotify (0.10.1) |     rb-inotify (0.10.1) | ||||||
|       ffi (~> 1.0) |       ffi (~> 1.0) | ||||||
| @ -74,6 +79,7 @@ PLATFORMS | |||||||
| DEPENDENCIES | DEPENDENCIES | ||||||
|   jekyll (~> 4.0) |   jekyll (~> 4.0) | ||||||
|   jekyll-last-modified-at! |   jekyll-last-modified-at! | ||||||
|  |   nokogiri | ||||||
|   webrick (~> 1.7) |   webrick (~> 1.7) | ||||||
| 
 | 
 | ||||||
| BUNDLED WITH | BUNDLED WITH | ||||||
|  | |||||||
| @ -10,6 +10,10 @@ baseurl:             '' | |||||||
| # not end with .html (such as Neocities), set this to 'true'. | # not end with .html (such as Neocities), set this to 'true'. | ||||||
| use_html_extension:  false | use_html_extension:  false | ||||||
| 
 | 
 | ||||||
|  | # Set to `true` to open non-internal links in new tabs, or | ||||||
|  | # set to `false` to open non-internal links in current tab. | ||||||
|  | open_external_links_in_new_tab: true | ||||||
|  | 
 | ||||||
| permalink:           pretty | permalink:           pretty | ||||||
| relative_permalinks: false | relative_permalinks: false | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -23,7 +23,13 @@ Since the Web is all about HTML, you can always use plain HTML if you want, like | |||||||
| 
 | 
 | ||||||
| Of course, you can also link to external websites, like this: [this is a link to Wikipedia](https://wikipedia.org/). Again, you can use plain HTML if you prefer. | Of course, you can also link to external websites, like this: [this is a link to Wikipedia](https://wikipedia.org/). Again, you can use plain HTML if you prefer. | ||||||
| 
 | 
 | ||||||
| **Note about static hosts**: if you use a static host that doesn't support URLs that don't end with `.html` (such as Neocities), try changing the `use_html_extension` value to `true` in the `_config.yml` file and restart the Jekyll server (or re-build the site). This adds a `.html` extension to note URLs and may resolve issues with links. If you're still having trouble, I recommend using Netlify to host your digital garden: it's free, easy to use, and fully supports this template's features out of the box. | ### Site configuration | ||||||
|  | 
 | ||||||
|  | Some behavior is configurable by tweaking the `_config.yml` file. | ||||||
|  | 
 | ||||||
|  | **`use_html_extension`**: if you use a static host that doesn't support URLs that don't end with `.html` (such as Neocities), try changing the `use_html_extension` value to `true` in the `_config.yml` file and restart the Jekyll server (or re-build the site). This adds a `.html` extension to note URLs and may resolve issues with links. If you're still having trouble, I recommend using Netlify to host your digital garden: it's free, easy to use, and fully supports this template's features out of the box. | ||||||
|  | 
 | ||||||
|  | **`open_external_links_in_new_tab`**: when set to `true`, this makes external links open in new tabs. Set to `false` to open all links in the current tab. | ||||||
| 
 | 
 | ||||||
| ### Automatic bi-directional links | ### Automatic bi-directional links | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										16
									
								
								_plugins/open_external_links_in_new_tab.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								_plugins/open_external_links_in_new_tab.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | |||||||
|  | # frozen_string_literal: true | ||||||
|  | require 'nokogiri' | ||||||
|  | 
 | ||||||
|  | # If the configuration sets `open_external_links_in_new_tab` to a truthy value, | ||||||
|  | # add 'target=_blank' to anchor tags that don't have `internal-link` class | ||||||
|  | Jekyll::Hooks.register [:pages, :notes], :post_convert do |doc| | ||||||
|  |   open_external_links_in_new_tab = !!doc.site.config["open_external_links_in_new_tab"] | ||||||
|  | 
 | ||||||
|  |   if open_external_links_in_new_tab | ||||||
|  |     parsed_doc = Nokogiri::HTML(doc.content) | ||||||
|  |     parsed_doc.css("a:not(.internal-link)").each do |link| | ||||||
|  |       link.set_attribute('target', 'blank') | ||||||
|  |     end | ||||||
|  |     doc.content = parsed_doc.to_html | ||||||
|  |   end | ||||||
|  | end | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Maxime Vaillancourt
						Maxime Vaillancourt