postProcessHTML treats different types of HTML differently, and only renders special links for plain text blocks.
(rawHTML []byte, urlPrefix string, metas map[string]string)
| 243 | // postProcessHTML treats different types of HTML differently, |
| 244 | // and only renders special links for plain text blocks. |
| 245 | func postProcessHTML(rawHTML []byte, urlPrefix string, metas map[string]string) []byte { |
| 246 | startTags := make([]string, 0, 5) |
| 247 | buf := bytes.NewBuffer(nil) |
| 248 | tokenizer := html.NewTokenizer(bytes.NewReader(rawHTML)) |
| 249 | |
| 250 | outerLoop: |
| 251 | for html.ErrorToken != tokenizer.Next() { |
| 252 | token := tokenizer.Token() |
| 253 | switch token.Type { |
| 254 | case html.TextToken: |
| 255 | buf.Write(RenderSpecialLink([]byte(token.String()), urlPrefix, metas)) |
| 256 | |
| 257 | case html.StartTagToken: |
| 258 | tagName := token.Data |
| 259 | |
| 260 | if tagName == "img" { |
| 261 | wrapImgWithLink(urlPrefix, buf, token) |
| 262 | continue outerLoop |
| 263 | } |
| 264 | |
| 265 | buf.WriteString(token.String()) |
| 266 | // If this is an excluded tag, we skip processing all output until a close tag is encountered. |
| 267 | if strings.EqualFold("a", tagName) || strings.EqualFold("code", tagName) || strings.EqualFold("pre", tagName) { |
| 268 | stackNum := 1 |
| 269 | for html.ErrorToken != tokenizer.Next() { |
| 270 | token = tokenizer.Token() |
| 271 | |
| 272 | // Copy the token to the output verbatim |
| 273 | buf.WriteString(token.String()) |
| 274 | |
| 275 | // Stack number doesn't increase for tags without end tags. |
| 276 | if token.Type == html.StartTagToken && !com.IsSliceContainsStr(noEndTags, token.Data) { |
| 277 | stackNum++ |
| 278 | } |
| 279 | |
| 280 | // If this is the close tag to the outer-most, we are done |
| 281 | if token.Type == html.EndTagToken { |
| 282 | stackNum-- |
| 283 | if stackNum <= 0 && strings.EqualFold(tagName, token.Data) { |
| 284 | break |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | continue outerLoop |
| 289 | } |
| 290 | |
| 291 | if !com.IsSliceContainsStr(noEndTags, tagName) { |
| 292 | startTags = append(startTags, tagName) |
| 293 | } |
| 294 | |
| 295 | case html.EndTagToken: |
| 296 | if len(startTags) == 0 { |
| 297 | buf.WriteString(token.String()) |
| 298 | break |
| 299 | } |
| 300 | |
| 301 | buf.Write(leftAngleBracket) |
| 302 | buf.WriteString(startTags[len(startTags)-1]) |
no test coverage detected