This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.1.2 - add rel="nofollow" to external links from users with less th…
…an 10 reputation
- Loading branch information
Showing
6 changed files
with
304 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cleaner | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/BenLubar/htmlcleaner" | ||
"golang.org/x/net/html" | ||
"golang.org/x/net/html/atom" | ||
) | ||
|
||
func NoFollow(content string, base *url.URL) string { | ||
nodes := htmlcleaner.ParseDepth(content, 0) | ||
for _, n := range nodes { | ||
noFollow(n, base) | ||
} | ||
return htmlcleaner.Render(nodes...) | ||
} | ||
|
||
func noFollow(n *html.Node, base *url.URL) { | ||
if n.Type != html.ElementNode { | ||
return | ||
} | ||
|
||
for c := n.FirstChild; c != nil; c = c.NextSibling { | ||
noFollow(c, base) | ||
} | ||
|
||
if n.DataAtom == atom.A { | ||
for _, a := range n.Attr { | ||
if a.Key == "href" { | ||
u, err := base.Parse(a.Val) | ||
if err != nil || u.Host != base.Host || u.Scheme != base.Scheme { | ||
break | ||
} | ||
return | ||
} | ||
} | ||
|
||
for i, a := range n.Attr { | ||
if a.Key == "rel" { | ||
n.Attr[i].Val = "nofollow" | ||
return | ||
} | ||
} | ||
|
||
n.Attr = append(n.Attr, html.Attribute{ | ||
Key: "rel", | ||
Val: "nofollow", | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/BenLubar/nodebb-plugin-htmlcleaner/cleaner" | ||
"github.com/gopherjs/gopherjs/js" | ||
) | ||
|
||
// zero width non-breaking space is illegal anyway, so removing it won't cause | ||
// any problems as long as another plugin isn't using the same hack. | ||
var fixer = strings.NewReplacer("\n<", "\n\ufeff<") | ||
var remover = strings.NewReplacer("\ufeff", "") | ||
|
||
func fix(s string) string { | ||
func fix(s, uid string) string { | ||
return fixer.Replace(s) | ||
} | ||
|
||
func clean(s string) string { | ||
var nconfURL, _ = url.Parse(js.Module.Get("parent").Call("require", "nconf").Call("get", "url").String()) | ||
var user = js.Module.Get("parent").Call("require", "./user.js") | ||
|
||
func clean(s, uid string) (content string) { | ||
defer func() { | ||
rep := make(chan int, 1) | ||
if id := js.Global.Call("parseInt", uid, 10).Int(); id > 0 { | ||
user.Call("getUserField", id, "reputation", func(err *js.Error, reputation int) { | ||
if err != nil { | ||
reputation = 0 | ||
} | ||
rep <- reputation | ||
}) | ||
} else { | ||
rep <- 0 | ||
} | ||
|
||
if <-rep < 10 { | ||
content = cleaner.NoFollow(content, nconfURL) | ||
} | ||
}() | ||
|
||
return cleaner.Clean(remover.Replace(s)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.