Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace HtmlText with Links #6

Open
pgodwin opened this issue Apr 16, 2023 · 0 comments
Open

Replace HtmlText with Links #6

pgodwin opened this issue Apr 16, 2023 · 0 comments

Comments

@pgodwin
Copy link

pgodwin commented Apr 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant