BracketPipe icon indicating copy to clipboard operation
BracketPipe copied to clipboard

Replace HtmlText with Links

Open pgodwin opened this issue 1 year ago • 0 comments

I've used HtmlSanitizer as the basis for injecting links to keywords in content. Basically, when a TextToken comes up, it looks for keywords in the value and wraps a link around the keyword.

Unfortunately I don't think this is the right approach, as it seems to mess up the parent nodes somehow.

<ul><liTitle Here ><br><spanThis is a test result <a href="/search?q=foo">foo</a> <a href="/search?q=bar">bar</a> is also here....></span></li></ul>

This is the code I'm using to test inject links:

static HashSet<string> keywords = new HashSet<string>(new string[]  
{
    "foo",
    "bar"
});

private static HtmlText InjectLink(HtmlNode node, HtmlSanitizeSettings settings)
{
    var words = node.Value.Split(new char[] { ' ' });
    for (int i = 0; i < words.Length; i++)
    {
        var word = words[i];
        if (keywords.Contains(word))
        {
            var link = $"<a href=\"/search?q={word}\">{word}</a>";
            words[i] = link;
        }
    }
    var value = string.Join(" ", words);
    var text = new HtmlText(node.Position, value)
    {
        Encode = false
    };
    return text;
}

Which I call from a modification of the Sanitize() function:

switch (token.Type)
{
    case HtmlTokenType.Text:
        if (removeDepth < 0)
        {
            if (inStyle)
                yield return new HtmlText(token.Position, SanitizeCss(token.Value, settings, true));
            else
                // yield return token;
                yield return InjectLink(token, settings);
                
        }
        break;
//...

Test Html:

<ul><li>Title Here <br><span>This is a test result foo bar is also here....</span></li></ul>

Expected Result:

<ul><li>Title Here <br><span>This is a test result <a href=""/search/q?=foo"">foo</a> <a href=""/search/q?=bar"">bar</a> is also here....</span></li></ul>

Actual Result:

<ul><liTitle Here ><br><spanThis is a test result <a href="/search?q=foo">foo</a> <a href="/search?q=bar">bar</a> is also here....></span></li></ul>

Example Project: CleanAndLink.zip

pgodwin avatar Apr 16 '23 23:04 pgodwin