You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1033 lines
29 KiB

Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Check commit message hashes before making links (#7713) * Check commit message hashes before making links Previously, when formatting commit messages, anything that looked like SHA1 hashes was turned into a link using regex. This meant that certain phrases or numbers such as `777777` or `deadbeef` could be recognized as a commit even if the repository has no commit with those hashes. This change will make it so that anything that looks like a SHA1 hash using regex will then also be checked to ensure that there is a commit in the repository with that hash before making a link. Signed-off-by: Gary Kim <gary@garykim.dev> * Use gogit to check if commit exists This commit modifies the commit hash check in the render for commit messages to use gogit for better performance. Signed-off-by: Gary Kim <gary@garykim.dev> * Make code cleaner Signed-off-by: Gary Kim <gary@garykim.dev> * Use rev-parse to check if commit exists Signed-off-by: Gary Kim <gary@garykim.dev> * Add and modify tests for checking hashes in html link rendering Signed-off-by: Gary Kim <gary@garykim.dev> * Return error in sha1CurrentPatternProcessor Co-Authored-By: mrsdizzie <info@mrsdizzie.com> * Import Gitea log module Signed-off-by: Gary Kim <gary@garykim.dev> * Revert "Return error in sha1CurrentPatternProcessor" This reverts commit 28f561cac46ef7e51aa26aefcbe9aca4671366a6. Signed-off-by: Gary Kim <gary@garykim.dev> * Add debug logging to sha1CurrentPatternProcessor This will log errors by the git command run in sha1CurrentPatternProcessor if the error is one that was unexpected. Signed-off-by: Gary Kim <gary@garykim.dev>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Support unicode emojis and remove emojify.js (#11032) * Support unicode emojis and remove emojify.js This PR replaces all use of emojify.js and adds unicode emoji support to various areas of gitea. This works in a few ways: First it adds emoji parsing support into gitea itself. This allows us to * Render emojis from valid alias (:smile:) * Detect unicode emojis and let us put them in their own class with proper aria-labels and styling * Easily allow for custom "emoji" * Support all emoji rendering and features without javascript * Uses plain unicode and lets the system render in appropriate emoji font * Doesn't leave us relying on external sources for updates/fixes/features That same list of emoji is also used to create a json file which replaces the part of emojify.js that populates the emoji search tribute. This file is about 35KB with GZIP turned on and I've set it to load after the page renders to not hinder page load time (and this removes loading emojify.js also) For custom "emoji" it uses a pretty simple scheme of just looking for /emojis/img/name.png where name is something a user has put in the "allowed reactions" setting we already have. The gitea reaction that was previously hard coded into a forked copy of emojify.js is included and works as a custom reaction under this method. The emoji data sourced here is from https://github.com/github/gemoji which is the gem library Github uses for their emoji rendering (and a data source for other sites). So we should be able to easily render any emoji and :alias: that Github can, removing any errors from migrated content. They also update it as well, so we can sync when there are new unicode emoji lists released. I've included a slimmed down and slightly modified forked copy of https://github.com/knq/emoji to make up our own emoji module. The code is pretty straight forward and again allows us to have a lot of flexibility in what happens. I had seen a few comments about performance in some of the other threads if we render this ourselves, but there doesn't seem to be any issue here. In a test it can parse, convert, and render 1,000 emojis inside of a large markdown table in about 100ms on my laptop (which is many more emojis than will ever be in any normal issue). This also prevents any flickering and other weirdness from using javascript to render some things while using go for others. Not included here are image fall back URLS. I don't really think they are necessary for anything new being written in 2020. However, managing the emoji ourselves would allow us to add these as a feature later on if it seems necessary. Fixes: https://github.com/go-gitea/gitea/issues/9182 Fixes: https://github.com/go-gitea/gitea/issues/8974 Fixes: https://github.com/go-gitea/gitea/issues/8953 Fixes: https://github.com/go-gitea/gitea/issues/6628 Fixes: https://github.com/go-gitea/gitea/issues/5130 * add new shared function emojiHTML * don't increase emoji size in issue title * Update templates/repo/issue/view_content/add_reaction.tmpl Co-Authored-By: 6543 <6543@obermui.de> * Support for emoji rendering in various templates * Render code and review comments as they should be * Better way to handle mail subjects * insert unicode from tribute selection * Add template helper for plain text when needed * Use existing replace function I forgot about * Don't include emoji greater than Unicode Version 12 Only include emoji and aliases in JSON * Update build/generate-emoji.go * Tweak regex slightly to really match everything including random invisible characters. Run tests for every emoji we have * final updates * code review * code review * hard code gitea custom emoji to match previous behavior * Update .eslintrc Co-Authored-By: silverwind <me@silverwind.io> * disable preempt Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
4 years ago
Check commit message hashes before making links (#7713) * Check commit message hashes before making links Previously, when formatting commit messages, anything that looked like SHA1 hashes was turned into a link using regex. This meant that certain phrases or numbers such as `777777` or `deadbeef` could be recognized as a commit even if the repository has no commit with those hashes. This change will make it so that anything that looks like a SHA1 hash using regex will then also be checked to ensure that there is a commit in the repository with that hash before making a link. Signed-off-by: Gary Kim <gary@garykim.dev> * Use gogit to check if commit exists This commit modifies the commit hash check in the render for commit messages to use gogit for better performance. Signed-off-by: Gary Kim <gary@garykim.dev> * Make code cleaner Signed-off-by: Gary Kim <gary@garykim.dev> * Use rev-parse to check if commit exists Signed-off-by: Gary Kim <gary@garykim.dev> * Add and modify tests for checking hashes in html link rendering Signed-off-by: Gary Kim <gary@garykim.dev> * Return error in sha1CurrentPatternProcessor Co-Authored-By: mrsdizzie <info@mrsdizzie.com> * Import Gitea log module Signed-off-by: Gary Kim <gary@garykim.dev> * Revert "Return error in sha1CurrentPatternProcessor" This reverts commit 28f561cac46ef7e51aa26aefcbe9aca4671366a6. Signed-off-by: Gary Kim <gary@garykim.dev> * Add debug logging to sha1CurrentPatternProcessor This will log errors by the git command run in sha1CurrentPatternProcessor if the error is one that was unexpected. Signed-off-by: Gary Kim <gary@garykim.dev>
4 years ago
Check commit message hashes before making links (#7713) * Check commit message hashes before making links Previously, when formatting commit messages, anything that looked like SHA1 hashes was turned into a link using regex. This meant that certain phrases or numbers such as `777777` or `deadbeef` could be recognized as a commit even if the repository has no commit with those hashes. This change will make it so that anything that looks like a SHA1 hash using regex will then also be checked to ensure that there is a commit in the repository with that hash before making a link. Signed-off-by: Gary Kim <gary@garykim.dev> * Use gogit to check if commit exists This commit modifies the commit hash check in the render for commit messages to use gogit for better performance. Signed-off-by: Gary Kim <gary@garykim.dev> * Make code cleaner Signed-off-by: Gary Kim <gary@garykim.dev> * Use rev-parse to check if commit exists Signed-off-by: Gary Kim <gary@garykim.dev> * Add and modify tests for checking hashes in html link rendering Signed-off-by: Gary Kim <gary@garykim.dev> * Return error in sha1CurrentPatternProcessor Co-Authored-By: mrsdizzie <info@mrsdizzie.com> * Import Gitea log module Signed-off-by: Gary Kim <gary@garykim.dev> * Revert "Return error in sha1CurrentPatternProcessor" This reverts commit 28f561cac46ef7e51aa26aefcbe9aca4671366a6. Signed-off-by: Gary Kim <gary@garykim.dev> * Add debug logging to sha1CurrentPatternProcessor This will log errors by the git command run in sha1CurrentPatternProcessor if the error is one that was unexpected. Signed-off-by: Gary Kim <gary@garykim.dev>
4 years ago
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markup
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/url"
  9. "path"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/emoji"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/markup/common"
  18. "code.gitea.io/gitea/modules/references"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/util"
  21. "github.com/unknwon/com"
  22. "golang.org/x/net/html"
  23. "golang.org/x/net/html/atom"
  24. "mvdan.cc/xurls/v2"
  25. )
  26. // Issue name styles
  27. const (
  28. IssueNameStyleNumeric = "numeric"
  29. IssueNameStyleAlphanumeric = "alphanumeric"
  30. )
  31. var (
  32. // NOTE: All below regex matching do not perform any extra validation.
  33. // Thus a link is produced even if the linked entity does not exist.
  34. // While fast, this is also incorrect and lead to false positives.
  35. // TODO: fix invalid linking issue
  36. // sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
  37. // Although SHA1 hashes are 40 chars long, the regex matches the hash from 7 to 40 chars in length
  38. // so that abbreviated hash links can be used as well. This matches git and github useability.
  39. sha1CurrentPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-f]{7,40})(?:\s|$|\)|\]|\.(\s|$))`)
  40. // shortLinkPattern matches short but difficult to parse [[name|link|arg=test]] syntax
  41. shortLinkPattern = regexp.MustCompile(`\[\[(.*?)\]\](\w*)`)
  42. // anySHA1Pattern allows to split url containing SHA into parts
  43. anySHA1Pattern = regexp.MustCompile(`https?://(?:\S+/){4}([0-9a-f]{40})(/[^#\s]+)?(#\S+)?`)
  44. validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://`)
  45. // While this email regex is definitely not perfect and I'm sure you can come up
  46. // with edge cases, it is still accepted by the CommonMark specification, as
  47. // well as the HTML5 spec:
  48. // http://spec.commonmark.org/0.28/#email-address
  49. // https://html.spec.whatwg.org/multipage/input.html#e-mail-state-(type%3Demail)
  50. emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|\\.(\\s|$))")
  51. // blackfriday extensions create IDs like fn:user-content-footnote
  52. blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
  53. // EmojiShortCodeRegex find emoji by alias like :smile:
  54. EmojiShortCodeRegex = regexp.MustCompile(`\:[\w\+\-]+\:{1}`)
  55. )
  56. // CSS class for action keywords (e.g. "closes: #1")
  57. const keywordClass = "issue-keyword"
  58. // regexp for full links to issues/pulls
  59. var issueFullPattern *regexp.Regexp
  60. // IsLink reports whether link fits valid format.
  61. func IsLink(link []byte) bool {
  62. return isLink(link)
  63. }
  64. // isLink reports whether link fits valid format.
  65. func isLink(link []byte) bool {
  66. return validLinksPattern.Match(link)
  67. }
  68. func isLinkStr(link string) bool {
  69. return validLinksPattern.MatchString(link)
  70. }
  71. func getIssueFullPattern() *regexp.Regexp {
  72. if issueFullPattern == nil {
  73. appURL := setting.AppURL
  74. if len(appURL) > 0 && appURL[len(appURL)-1] != '/' {
  75. appURL += "/"
  76. }
  77. issueFullPattern = regexp.MustCompile(appURL +
  78. `\w+/\w+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#]\S+.(\S+)?)?\b`)
  79. }
  80. return issueFullPattern
  81. }
  82. // CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
  83. func CustomLinkURLSchemes(schemes []string) {
  84. schemes = append(schemes, "http", "https")
  85. withAuth := make([]string, 0, len(schemes))
  86. validScheme := regexp.MustCompile(`^[a-z]+$`)
  87. for _, s := range schemes {
  88. if !validScheme.MatchString(s) {
  89. continue
  90. }
  91. without := false
  92. for _, sna := range xurls.SchemesNoAuthority {
  93. if s == sna {
  94. without = true
  95. break
  96. }
  97. }
  98. if without {
  99. s += ":"
  100. } else {
  101. s += "://"
  102. }
  103. withAuth = append(withAuth, s)
  104. }
  105. common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
  106. }
  107. // IsSameDomain checks if given url string has the same hostname as current Gitea instance
  108. func IsSameDomain(s string) bool {
  109. if strings.HasPrefix(s, "/") {
  110. return true
  111. }
  112. if uapp, err := url.Parse(setting.AppURL); err == nil {
  113. if u, err := url.Parse(s); err == nil {
  114. return u.Host == uapp.Host
  115. }
  116. return false
  117. }
  118. return false
  119. }
  120. type postProcessError struct {
  121. context string
  122. err error
  123. }
  124. func (p *postProcessError) Error() string {
  125. return "PostProcess: " + p.context + ", " + p.err.Error()
  126. }
  127. type processor func(ctx *postProcessCtx, node *html.Node)
  128. var defaultProcessors = []processor{
  129. fullIssuePatternProcessor,
  130. fullSha1PatternProcessor,
  131. shortLinkProcessor,
  132. linkProcessor,
  133. mentionProcessor,
  134. issueIndexPatternProcessor,
  135. sha1CurrentPatternProcessor,
  136. emailAddressProcessor,
  137. emojiProcessor,
  138. emojiShortCodeProcessor,
  139. }
  140. type postProcessCtx struct {
  141. metas map[string]string
  142. urlPrefix string
  143. isWikiMarkdown bool
  144. // processors used by this context.
  145. procs []processor
  146. }
  147. // PostProcess does the final required transformations to the passed raw HTML
  148. // data, and ensures its validity. Transformations include: replacing links and
  149. // emails with HTML links, parsing shortlinks in the format of [[Link]], like
  150. // MediaWiki, linking issues in the format #ID, and mentions in the format
  151. // @user, and others.
  152. func PostProcess(
  153. rawHTML []byte,
  154. urlPrefix string,
  155. metas map[string]string,
  156. isWikiMarkdown bool,
  157. ) ([]byte, error) {
  158. // create the context from the parameters
  159. ctx := &postProcessCtx{
  160. metas: metas,
  161. urlPrefix: urlPrefix,
  162. isWikiMarkdown: isWikiMarkdown,
  163. procs: defaultProcessors,
  164. }
  165. return ctx.postProcess(rawHTML)
  166. }
  167. var commitMessageProcessors = []processor{
  168. fullIssuePatternProcessor,
  169. fullSha1PatternProcessor,
  170. linkProcessor,
  171. mentionProcessor,
  172. issueIndexPatternProcessor,
  173. sha1CurrentPatternProcessor,
  174. emailAddressProcessor,
  175. emojiProcessor,
  176. emojiShortCodeProcessor,
  177. }
  178. // RenderCommitMessage will use the same logic as PostProcess, but will disable
  179. // the shortLinkProcessor and will add a defaultLinkProcessor if defaultLink is
  180. // set, which changes every text node into a link to the passed default link.
  181. func RenderCommitMessage(
  182. rawHTML []byte,
  183. urlPrefix, defaultLink string,
  184. metas map[string]string,
  185. ) ([]byte, error) {
  186. ctx := &postProcessCtx{
  187. metas: metas,
  188. urlPrefix: urlPrefix,
  189. procs: commitMessageProcessors,
  190. }
  191. if defaultLink != "" {
  192. // we don't have to fear data races, because being
  193. // commitMessageProcessors of fixed len and cap, every time we append
  194. // something to it the slice is realloc+copied, so append always
  195. // generates the slice ex-novo.
  196. ctx.procs = append(ctx.procs, genDefaultLinkProcessor(defaultLink))
  197. }
  198. return ctx.postProcess(rawHTML)
  199. }
  200. var commitMessageSubjectProcessors = []processor{
  201. fullIssuePatternProcessor,
  202. fullSha1PatternProcessor,
  203. linkProcessor,
  204. mentionProcessor,
  205. issueIndexPatternProcessor,
  206. sha1CurrentPatternProcessor,
  207. emojiShortCodeProcessor,
  208. emojiProcessor,
  209. }
  210. var emojiProcessors = []processor{
  211. emojiShortCodeProcessor,
  212. emojiProcessor,
  213. }
  214. // RenderCommitMessageSubject will use the same logic as PostProcess and
  215. // RenderCommitMessage, but will disable the shortLinkProcessor and
  216. // emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set,
  217. // which changes every text node into a link to the passed default link.
  218. func RenderCommitMessageSubject(
  219. rawHTML []byte,
  220. urlPrefix, defaultLink string,
  221. metas map[string]string,
  222. ) ([]byte, error) {
  223. ctx := &postProcessCtx{
  224. metas: metas,
  225. urlPrefix: urlPrefix,
  226. procs: commitMessageSubjectProcessors,
  227. }
  228. if defaultLink != "" {
  229. // we don't have to fear data races, because being
  230. // commitMessageSubjectProcessors of fixed len and cap, every time we
  231. // append something to it the slice is realloc+copied, so append always
  232. // generates the slice ex-novo.
  233. ctx.procs = append(ctx.procs, genDefaultLinkProcessor(defaultLink))
  234. }
  235. return ctx.postProcess(rawHTML)
  236. }
  237. // RenderDescriptionHTML will use similar logic as PostProcess, but will
  238. // use a single special linkProcessor.
  239. func RenderDescriptionHTML(
  240. rawHTML []byte,
  241. urlPrefix string,
  242. metas map[string]string,
  243. ) ([]byte, error) {
  244. ctx := &postProcessCtx{
  245. metas: metas,
  246. urlPrefix: urlPrefix,
  247. procs: []processor{
  248. descriptionLinkProcessor,
  249. emojiShortCodeProcessor,
  250. emojiProcessor,
  251. },
  252. }
  253. return ctx.postProcess(rawHTML)
  254. }
  255. // RenderEmoji for when we want to just process emoji and shortcodes
  256. // in various places it isn't already run through the normal markdown procesor
  257. func RenderEmoji(
  258. rawHTML []byte,
  259. ) ([]byte, error) {
  260. ctx := &postProcessCtx{
  261. procs: emojiProcessors,
  262. }
  263. return ctx.postProcess(rawHTML)
  264. }
  265. var byteBodyTag = []byte("<body>")
  266. var byteBodyTagClosing = []byte("</body>")
  267. func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
  268. if ctx.procs == nil {
  269. ctx.procs = defaultProcessors
  270. }
  271. // give a generous extra 50 bytes
  272. res := make([]byte, 0, len(rawHTML)+50)
  273. res = append(res, byteBodyTag...)
  274. res = append(res, rawHTML...)
  275. res = append(res, byteBodyTagClosing...)
  276. // parse the HTML
  277. nodes, err := html.ParseFragment(bytes.NewReader(res), nil)
  278. if err != nil {
  279. return nil, &postProcessError{"invalid HTML", err}
  280. }
  281. for _, node := range nodes {
  282. ctx.visitNode(node, true)
  283. }
  284. // Create buffer in which the data will be placed again. We know that the
  285. // length will be at least that of res; to spare a few alloc+copy, we
  286. // reuse res, resetting its length to 0.
  287. buf := bytes.NewBuffer(res[:0])
  288. // Render everything to buf.
  289. for _, node := range nodes {
  290. err = html.Render(buf, node)
  291. if err != nil {
  292. return nil, &postProcessError{"error rendering processed HTML", err}
  293. }
  294. }
  295. // remove initial parts - because Render creates a whole HTML page.
  296. res = buf.Bytes()
  297. res = res[bytes.Index(res, byteBodyTag)+len(byteBodyTag) : bytes.LastIndex(res, byteBodyTagClosing)]
  298. // Everything done successfully, return parsed data.
  299. return res, nil
  300. }
  301. func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
  302. // Add user-content- to IDs if they don't already have them
  303. for idx, attr := range node.Attr {
  304. if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
  305. node.Attr[idx].Val = "user-content-" + attr.Val
  306. }
  307. if attr.Key == "class" && attr.Val == "emoji" {
  308. visitText = false
  309. }
  310. }
  311. // We ignore code, pre and already generated links.
  312. switch node.Type {
  313. case html.TextNode:
  314. if visitText {
  315. ctx.textNode(node)
  316. }
  317. case html.ElementNode:
  318. if node.Data == "img" {
  319. attrs := node.Attr
  320. for idx, attr := range attrs {
  321. if attr.Key != "src" {
  322. continue
  323. }
  324. link := []byte(attr.Val)
  325. if len(link) > 0 && !IsLink(link) {
  326. prefix := ctx.urlPrefix
  327. if ctx.isWikiMarkdown {
  328. prefix = util.URLJoin(prefix, "wiki", "raw")
  329. }
  330. prefix = strings.Replace(prefix, "/src/", "/media/", 1)
  331. lnk := string(link)
  332. lnk = util.URLJoin(prefix, lnk)
  333. link = []byte(lnk)
  334. }
  335. node.Attr[idx].Val = string(link)
  336. }
  337. } else if node.Data == "a" {
  338. visitText = false
  339. } else if node.Data == "code" || node.Data == "pre" {
  340. return
  341. } else if node.Data == "i" {
  342. for _, attr := range node.Attr {
  343. if attr.Key != "class" {
  344. continue
  345. }
  346. classes := strings.Split(attr.Val, " ")
  347. for i, class := range classes {
  348. if class == "icon" {
  349. classes[0], classes[i] = classes[i], classes[0]
  350. attr.Val = strings.Join(classes, " ")
  351. // Remove all children of icons
  352. child := node.FirstChild
  353. for child != nil {
  354. node.RemoveChild(child)
  355. child = node.FirstChild
  356. }
  357. break
  358. }
  359. }
  360. }
  361. }
  362. for n := node.FirstChild; n != nil; n = n.NextSibling {
  363. ctx.visitNode(n, visitText)
  364. }
  365. }
  366. // ignore everything else
  367. }
  368. // textNode runs the passed node through various processors, in order to handle
  369. // all kinds of special links handled by the post-processing.
  370. func (ctx *postProcessCtx) textNode(node *html.Node) {
  371. for _, processor := range ctx.procs {
  372. processor(ctx, node)
  373. }
  374. }
  375. // createKeyword() renders a highlighted version of an action keyword
  376. func createKeyword(content string) *html.Node {
  377. span := &html.Node{
  378. Type: html.ElementNode,
  379. Data: atom.Span.String(),
  380. Attr: []html.Attribute{},
  381. }
  382. span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: keywordClass})
  383. text := &html.Node{
  384. Type: html.TextNode,
  385. Data: content,
  386. }
  387. span.AppendChild(text)
  388. return span
  389. }
  390. func createEmoji(content, class, name string) *html.Node {
  391. span := &html.Node{
  392. Type: html.ElementNode,
  393. Data: atom.Span.String(),
  394. Attr: []html.Attribute{},
  395. }
  396. if class != "" {
  397. span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: class})
  398. }
  399. if name != "" {
  400. span.Attr = append(span.Attr, html.Attribute{Key: "aria-label", Val: name})
  401. }
  402. text := &html.Node{
  403. Type: html.TextNode,
  404. Data: content,
  405. }
  406. span.AppendChild(text)
  407. return span
  408. }
  409. func createCustomEmoji(alias, class string) *html.Node {
  410. span := &html.Node{
  411. Type: html.ElementNode,
  412. Data: atom.Span.String(),
  413. Attr: []html.Attribute{},
  414. }
  415. if class != "" {
  416. span.Attr = append(span.Attr, html.Attribute{Key: "class", Val: class})
  417. span.Attr = append(span.Attr, html.Attribute{Key: "aria-label", Val: alias})
  418. }
  419. img := &html.Node{
  420. Type: html.ElementNode,
  421. DataAtom: atom.Img,
  422. Data: "img",
  423. Attr: []html.Attribute{},
  424. }
  425. if class != "" {
  426. img.Attr = append(img.Attr, html.Attribute{Key: "alt", Val: fmt.Sprintf(`:%s:`, alias)})
  427. img.Attr = append(img.Attr, html.Attribute{Key: "src", Val: fmt.Sprintf(`%s/img/emoji/%s.png`, setting.StaticURLPrefix, alias)})
  428. }
  429. span.AppendChild(img)
  430. return span
  431. }
  432. func createLink(href, content, class string) *html.Node {
  433. a := &html.Node{
  434. Type: html.ElementNode,
  435. Data: atom.A.String(),
  436. Attr: []html.Attribute{{Key: "href", Val: href}},
  437. }
  438. if class != "" {
  439. a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
  440. }
  441. text := &html.Node{
  442. Type: html.TextNode,
  443. Data: content,
  444. }
  445. a.AppendChild(text)
  446. return a
  447. }
  448. func createCodeLink(href, content, class string) *html.Node {
  449. a := &html.Node{
  450. Type: html.ElementNode,
  451. Data: atom.A.String(),
  452. Attr: []html.Attribute{{Key: "href", Val: href}},
  453. }
  454. if class != "" {
  455. a.Attr = append(a.Attr, html.Attribute{Key: "class", Val: class})
  456. }
  457. text := &html.Node{
  458. Type: html.TextNode,
  459. Data: content,
  460. }
  461. code := &html.Node{
  462. Type: html.ElementNode,
  463. Data: atom.Code.String(),
  464. Attr: []html.Attribute{{Key: "class", Val: "nohighlight"}},
  465. }
  466. code.AppendChild(text)
  467. a.AppendChild(code)
  468. return a
  469. }
  470. // replaceContent takes text node, and in its content it replaces a section of
  471. // it with the specified newNode.
  472. func replaceContent(node *html.Node, i, j int, newNode *html.Node) {
  473. replaceContentList(node, i, j, []*html.Node{newNode})
  474. }
  475. // replaceContentList takes text node, and in its content it replaces a section of
  476. // it with the specified newNodes. An example to visualize how this can work can
  477. // be found here: https://play.golang.org/p/5zP8NnHZ03s
  478. func replaceContentList(node *html.Node, i, j int, newNodes []*html.Node) {
  479. // get the data before and after the match
  480. before := node.Data[:i]
  481. after := node.Data[j:]
  482. // Replace in the current node the text, so that it is only what it is
  483. // supposed to have.
  484. node.Data = before
  485. // Get the current next sibling, before which we place the replaced data,
  486. // and after that we place the new text node.
  487. nextSibling := node.NextSibling
  488. for _, n := range newNodes {
  489. node.Parent.InsertBefore(n, nextSibling)
  490. }
  491. if after != "" {
  492. node.Parent.InsertBefore(&html.Node{
  493. Type: html.TextNode,
  494. Data: after,
  495. }, nextSibling)
  496. }
  497. }
  498. func mentionProcessor(ctx *postProcessCtx, node *html.Node) {
  499. // We replace only the first mention; other mentions will be addressed later
  500. found, loc := references.FindFirstMentionBytes([]byte(node.Data))
  501. if !found {
  502. return
  503. }
  504. mention := node.Data[loc.Start:loc.End]
  505. var teams string
  506. teams, ok := ctx.metas["teams"]
  507. if ok && strings.Contains(teams, ","+strings.ToLower(mention[1:])+",") {
  508. replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, "org", ctx.metas["org"], "teams", mention[1:]), mention, "mention"))
  509. } else {
  510. replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mention[1:]), mention, "mention"))
  511. }
  512. }
  513. func shortLinkProcessor(ctx *postProcessCtx, node *html.Node) {
  514. shortLinkProcessorFull(ctx, node, false)
  515. }
  516. func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) {
  517. m := shortLinkPattern.FindStringSubmatchIndex(node.Data)
  518. if m == nil {
  519. return
  520. }
  521. content := node.Data[m[2]:m[3]]
  522. tail := node.Data[m[4]:m[5]]
  523. props := make(map[string]string)
  524. // MediaWiki uses [[link|text]], while GitHub uses [[text|link]]
  525. // It makes page handling terrible, but we prefer GitHub syntax
  526. // And fall back to MediaWiki only when it is obvious from the look
  527. // Of text and link contents
  528. sl := strings.Split(content, "|")
  529. for _, v := range sl {
  530. if equalPos := strings.IndexByte(v, '='); equalPos == -1 {
  531. // There is no equal in this argument; this is a mandatory arg
  532. if props["name"] == "" {
  533. if isLinkStr(v) {
  534. // If we clearly see it is a link, we save it so
  535. // But first we need to ensure, that if both mandatory args provided
  536. // look like links, we stick to GitHub syntax
  537. if props["link"] != "" {
  538. props["name"] = props["link"]
  539. }
  540. props["link"] = strings.TrimSpace(v)
  541. } else {
  542. props["name"] = v
  543. }
  544. } else {
  545. props["link"] = strings.TrimSpace(v)
  546. }
  547. } else {
  548. // There is an equal; optional argument.
  549. sep := strings.IndexByte(v, '=')
  550. key, val := v[:sep], html.UnescapeString(v[sep+1:])
  551. // When parsing HTML, x/net/html will change all quotes which are
  552. // not used for syntax into UTF-8 quotes. So checking val[0] won't
  553. // be enough, since that only checks a single byte.
  554. if (strings.HasPrefix(val, "“") && strings.HasSuffix(val, "”")) ||
  555. (strings.HasPrefix(val, "‘") && strings.HasSuffix(val, "’")) {
  556. const lenQuote = len("‘")
  557. val = val[lenQuote : len(val)-lenQuote]
  558. } else if (strings.HasPrefix(val, "\"") && strings.HasSuffix(val, "\"")) ||
  559. (strings.HasPrefix(val, "'") && strings.HasSuffix(val, "'")) {
  560. val = val[1 : len(val)-1]
  561. } else if strings.HasPrefix(val, "'") && strings.HasSuffix(val, "’") {
  562. const lenQuote = len("‘")
  563. val = val[1 : len(val)-lenQuote]
  564. }
  565. props[key] = val
  566. }
  567. }
  568. var name, link string
  569. if props["link"] != "" {
  570. link = props["link"]
  571. } else if props["name"] != "" {
  572. link = props["name"]
  573. }
  574. if props["title"] != "" {
  575. name = props["title"]
  576. } else if props["name"] != "" {
  577. name = props["name"]
  578. } else {
  579. name = link
  580. }
  581. name += tail
  582. image := false
  583. switch ext := filepath.Ext(link); ext {
  584. // fast path: empty string, ignore
  585. case "":
  586. break
  587. case ".jpg", ".jpeg", ".png", ".tif", ".tiff", ".webp", ".gif", ".bmp", ".ico", ".svg":
  588. image = true
  589. }
  590. childNode := &html.Node{}
  591. linkNode := &html.Node{
  592. FirstChild: childNode,
  593. LastChild: childNode,
  594. Type: html.ElementNode,
  595. Data: "a",
  596. DataAtom: atom.A,
  597. }
  598. childNode.Parent = linkNode
  599. absoluteLink := isLinkStr(link)
  600. if !absoluteLink {
  601. if image {
  602. link = strings.Replace(link, " ", "+", -1)
  603. } else {
  604. link = strings.Replace(link, " ", "-", -1)
  605. }
  606. if !strings.Contains(link, "/") {
  607. link = url.PathEscape(link)
  608. }
  609. }
  610. urlPrefix := ctx.urlPrefix
  611. if image {
  612. if !absoluteLink {
  613. if IsSameDomain(urlPrefix) {
  614. urlPrefix = strings.Replace(urlPrefix, "/src/", "/raw/", 1)
  615. }
  616. if ctx.isWikiMarkdown {
  617. link = util.URLJoin("wiki", "raw", link)
  618. }
  619. link = util.URLJoin(urlPrefix, link)
  620. }
  621. title := props["title"]
  622. if title == "" {
  623. title = props["alt"]
  624. }
  625. if title == "" {
  626. title = path.Base(name)
  627. }
  628. alt := props["alt"]
  629. if alt == "" {
  630. alt = name
  631. }
  632. // make the childNode an image - if we can, we also place the alt
  633. childNode.Type = html.ElementNode
  634. childNode.Data = "img"
  635. childNode.DataAtom = atom.Img
  636. childNode.Attr = []html.Attribute{
  637. {Key: "src", Val: link},
  638. {Key: "title", Val: title},
  639. {Key: "alt", Val: alt},
  640. }
  641. if alt == "" {
  642. childNode.Attr = childNode.Attr[:2]
  643. }
  644. } else {
  645. if !absoluteLink {
  646. if ctx.isWikiMarkdown {
  647. link = util.URLJoin("wiki", link)
  648. }
  649. link = util.URLJoin(urlPrefix, link)
  650. }
  651. childNode.Type = html.TextNode
  652. childNode.Data = name
  653. }
  654. if noLink {
  655. linkNode = childNode
  656. } else {
  657. linkNode.Attr = []html.Attribute{{Key: "href", Val: link}}
  658. }
  659. replaceContent(node, m[0], m[1], linkNode)
  660. }
  661. func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) {
  662. if ctx.metas == nil {
  663. return
  664. }
  665. m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
  666. if m == nil {
  667. return
  668. }
  669. link := node.Data[m[0]:m[1]]
  670. id := "#" + node.Data[m[2]:m[3]]
  671. // extract repo and org name from matched link like
  672. // http://localhost:3000/gituser/myrepo/issues/1
  673. linkParts := strings.Split(path.Clean(link), "/")
  674. matchOrg := linkParts[len(linkParts)-4]
  675. matchRepo := linkParts[len(linkParts)-3]
  676. if matchOrg == ctx.metas["user"] && matchRepo == ctx.metas["repo"] {
  677. // TODO if m[4]:m[5] is not nil, then link is to a comment,
  678. // and we should indicate that in the text somehow
  679. replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
  680. } else {
  681. orgRepoID := matchOrg + "/" + matchRepo + id
  682. replaceContent(node, m[0], m[1], createLink(link, orgRepoID, "ref-issue"))
  683. }
  684. }
  685. func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
  686. if ctx.metas == nil {
  687. return
  688. }
  689. var (
  690. found bool
  691. ref *references.RenderizableReference
  692. )
  693. _, exttrack := ctx.metas["format"]
  694. alphanum := ctx.metas["style"] == IssueNameStyleAlphanumeric
  695. // Repos with external issue trackers might still need to reference local PRs
  696. // We need to concern with the first one that shows up in the text, whichever it is
  697. found, ref = references.FindRenderizableReferenceNumeric(node.Data, exttrack && alphanum)
  698. if exttrack && alphanum {
  699. if found2, ref2 := references.FindRenderizableReferenceAlphanumeric(node.Data); found2 {
  700. if !found || ref2.RefLocation.Start < ref.RefLocation.Start {
  701. found = true
  702. ref = ref2
  703. }
  704. }
  705. }
  706. if !found {
  707. return
  708. }
  709. var link *html.Node
  710. reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
  711. if exttrack && !ref.IsPull {
  712. ctx.metas["index"] = ref.Issue
  713. link = createLink(com.Expand(ctx.metas["format"], ctx.metas), reftext, "ref-issue")
  714. } else {
  715. // Path determines the type of link that will be rendered. It's unknown at this point whether
  716. // the linked item is actually a PR or an issue. Luckily it's of no real consequence because
  717. // Gitea will redirect on click as appropriate.
  718. path := "issues"
  719. if ref.IsPull {
  720. path = "pulls"
  721. }
  722. if ref.Owner == "" {
  723. link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], path, ref.Issue), reftext, "ref-issue")
  724. } else {
  725. link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, path, ref.Issue), reftext, "ref-issue")
  726. }
  727. }
  728. if ref.Action == references.XRefActionNone {
  729. replaceContent(node, ref.RefLocation.Start, ref.RefLocation.End, link)
  730. return
  731. }
  732. // Decorate action keywords if actionable
  733. var keyword *html.Node
  734. if references.IsXrefActionable(ref, exttrack, alphanum) {
  735. keyword = createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End])
  736. } else {
  737. keyword = &html.Node{
  738. Type: html.TextNode,
  739. Data: node.Data[ref.ActionLocation.Start:ref.ActionLocation.End],
  740. }
  741. }
  742. spaces := &html.Node{
  743. Type: html.TextNode,
  744. Data: node.Data[ref.ActionLocation.End:ref.RefLocation.Start],
  745. }
  746. replaceContentList(node, ref.ActionLocation.Start, ref.RefLocation.End, []*html.Node{keyword, spaces, link})
  747. }
  748. // fullSha1PatternProcessor renders SHA containing URLs
  749. func fullSha1PatternProcessor(ctx *postProcessCtx, node *html.Node) {
  750. if ctx.metas == nil {
  751. return
  752. }
  753. m := anySHA1Pattern.FindStringSubmatchIndex(node.Data)
  754. if m == nil {
  755. return
  756. }
  757. urlFull := node.Data[m[0]:m[1]]
  758. text := base.ShortSha(node.Data[m[2]:m[3]])
  759. // 3rd capture group matches a optional path
  760. subpath := ""
  761. if m[5] > 0 {
  762. subpath = node.Data[m[4]:m[5]]
  763. }
  764. // 4th capture group matches a optional url hash
  765. hash := ""
  766. if m[7] > 0 {
  767. hash = node.Data[m[6]:m[7]][1:]
  768. }
  769. start := m[0]
  770. end := m[1]
  771. // If url ends in '.', it's very likely that it is not part of the
  772. // actual url but used to finish a sentence.
  773. if strings.HasSuffix(urlFull, ".") {
  774. end--
  775. urlFull = urlFull[:len(urlFull)-1]
  776. if hash != "" {
  777. hash = hash[:len(hash)-1]
  778. } else if subpath != "" {
  779. subpath = subpath[:len(subpath)-1]
  780. }
  781. }
  782. if subpath != "" {
  783. text += subpath
  784. }
  785. if hash != "" {
  786. text += " (" + hash + ")"
  787. }
  788. replaceContent(node, start, end, createCodeLink(urlFull, text, "commit"))
  789. }
  790. // emojiShortCodeProcessor for rendering text like :smile: into emoji
  791. func emojiShortCodeProcessor(ctx *postProcessCtx, node *html.Node) {
  792. m := EmojiShortCodeRegex.FindStringSubmatchIndex(node.Data)
  793. if m == nil {
  794. return
  795. }
  796. alias := node.Data[m[0]:m[1]]
  797. alias = strings.Replace(alias, ":", "", -1)
  798. converted := emoji.FromAlias(alias)
  799. if converted == nil {
  800. // check if this is a custom reaction
  801. s := strings.Join(setting.UI.Reactions, " ") + "gitea"
  802. if strings.Contains(s, alias) {
  803. replaceContent(node, m[0], m[1], createCustomEmoji(alias, "emoji"))
  804. return
  805. }
  806. return
  807. }
  808. replaceContent(node, m[0], m[1], createEmoji(converted.Emoji, "emoji", converted.Description))
  809. }
  810. // emoji processor to match emoji and add emoji class
  811. func emojiProcessor(ctx *postProcessCtx, node *html.Node) {
  812. m := emoji.FindEmojiSubmatchIndex(node.Data)
  813. if m == nil {
  814. return
  815. }
  816. codepoint := node.Data[m[0]:m[1]]
  817. val := emoji.FromCode(codepoint)
  818. if val != nil {
  819. replaceContent(node, m[0], m[1], createEmoji(codepoint, "emoji", val.Description))
  820. }
  821. }
  822. // sha1CurrentPatternProcessor renders SHA1 strings to corresponding links that
  823. // are assumed to be in the same repository.
  824. func sha1CurrentPatternProcessor(ctx *postProcessCtx, node *html.Node) {
  825. if ctx.metas == nil || ctx.metas["user"] == "" || ctx.metas["repo"] == "" || ctx.metas["repoPath"] == "" {
  826. return
  827. }
  828. m := sha1CurrentPattern.FindStringSubmatchIndex(node.Data)
  829. if m == nil {
  830. return
  831. }
  832. hash := node.Data[m[2]:m[3]]
  833. // The regex does not lie, it matches the hash pattern.
  834. // However, a regex cannot know if a hash actually exists or not.
  835. // We could assume that a SHA1 hash should probably contain alphas AND numerics
  836. // but that is not always the case.
  837. // Although unlikely, deadbeef and 1234567 are valid short forms of SHA1 hash
  838. // as used by git and github for linking and thus we have to do similar.
  839. // Because of this, we check to make sure that a matched hash is actually
  840. // a commit in the repository before making it a link.
  841. if _, err := git.NewCommand("rev-parse", "--verify", hash).RunInDirBytes(ctx.metas["repoPath"]); err != nil {
  842. if !strings.Contains(err.Error(), "fatal: Needed a single revision") {
  843. log.Debug("sha1CurrentPatternProcessor git rev-parse: %v", err)
  844. }
  845. return
  846. }
  847. replaceContent(node, m[2], m[3],
  848. createCodeLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "commit", hash), base.ShortSha(hash), "commit"))
  849. }
  850. // emailAddressProcessor replaces raw email addresses with a mailto: link.
  851. func emailAddressProcessor(ctx *postProcessCtx, node *html.Node) {
  852. m := emailRegex.FindStringSubmatchIndex(node.Data)
  853. if m == nil {
  854. return
  855. }
  856. mail := node.Data[m[2]:m[3]]
  857. replaceContent(node, m[2], m[3], createLink("mailto:"+mail, mail, "mailto"))
  858. }
  859. // linkProcessor creates links for any HTTP or HTTPS URL not captured by
  860. // markdown.
  861. func linkProcessor(ctx *postProcessCtx, node *html.Node) {
  862. m := common.LinkRegex.FindStringIndex(node.Data)
  863. if m == nil {
  864. return
  865. }
  866. uri := node.Data[m[0]:m[1]]
  867. replaceContent(node, m[0], m[1], createLink(uri, uri, "link"))
  868. }
  869. func genDefaultLinkProcessor(defaultLink string) processor {
  870. return func(ctx *postProcessCtx, node *html.Node) {
  871. ch := &html.Node{
  872. Parent: node,
  873. Type: html.TextNode,
  874. Data: node.Data,
  875. }
  876. node.Type = html.ElementNode
  877. node.Data = "a"
  878. node.DataAtom = atom.A
  879. node.Attr = []html.Attribute{
  880. {Key: "href", Val: defaultLink},
  881. {Key: "class", Val: "default-link"},
  882. }
  883. node.FirstChild, node.LastChild = ch, ch
  884. }
  885. }
  886. // descriptionLinkProcessor creates links for DescriptionHTML
  887. func descriptionLinkProcessor(ctx *postProcessCtx, node *html.Node) {
  888. m := common.LinkRegex.FindStringIndex(node.Data)
  889. if m == nil {
  890. return
  891. }
  892. uri := node.Data[m[0]:m[1]]
  893. replaceContent(node, m[0], m[1], createDescriptionLink(uri, uri))
  894. }
  895. func createDescriptionLink(href, content string) *html.Node {
  896. textNode := &html.Node{
  897. Type: html.TextNode,
  898. Data: content,
  899. }
  900. linkNode := &html.Node{
  901. FirstChild: textNode,
  902. LastChild: textNode,
  903. Type: html.ElementNode,
  904. Data: "a",
  905. DataAtom: atom.A,
  906. Attr: []html.Attribute{
  907. {Key: "href", Val: href},
  908. {Key: "target", Val: "_blank"},
  909. {Key: "rel", Val: "noopener noreferrer"},
  910. },
  911. }
  912. textNode.Parent = linkNode
  913. return linkNode
  914. }