#3 Scrub HTML Strings (Remove HTML Tags)
Requirements
Scrub HTML Strings (Remove HTML Tags)
These demos show how to strip all html from a string. You often need this in combination with crop or ellipsis to create teasers.
About the Scrub Service
This tutorial inherits from the Custom.Hybrid.Razor14
or the Custom.Hybrid.RazorTyped
base class.
This allows us to use Kit.Scrub
to access an IScrub
without having to use GetService<IScrub>
.
⬇️ Result | Source ➡️
Original
<h1>Introduction</h1><p>Welcome to this blog post</p><hr><p>beginning with xyz we'll tell you more</p>
which if cropped at 50 would be really messy
<h1>Introduction</h1><p>Welcome to this blog post<
⬇️ Result | Source ➡️
Introduction Welcome to this blog post beginning with xyz we'll tell you more
⬇️ Result | Source ➡️
Introduction Welcome to this blog post beginning…
Only scrub specified tags using IScrub.Only(source, tag)
⬇️ Result | Source ➡️
Scrub one tag:
<h2><span>The cool title</span></h2>
Scrub multiple tags:
<span>The cool title</span>
Scrub except the specified tags using IScrub.Except(source, tag)
⬇️ Result | Source ➡️
Scrub tags except one tag:
<h2>The cool title</h2>
Scrub tags except multiple tags:
<div><span>The cool title</span></div>
RazorBlade IScrub.Attributes(...), IScrub.Styles(), IScrub.Classes()
These demos show how to scrub attributes of html source.
Using IScrub.Classes(source)
you can scrub all classes.
⬇️ Result | Source ➡️
Scrub all classes from source:
<div ><strong >No</strong> classes allowed</div>
Using IScrub.Attributes(source, attributes)
you can scrub attributes.
⬇️ Result | Source ➡️
Scrub all attributes from source:
<div>No attributes allowed</div>
Scrub one attribute from source:
<div not-allowed class='removed'>No attributes allowed</div>
Scrub some attributes from source:
<div class='removed'>No attributes allowed</div>
Using IScrub.Styles(source)
you can scrub all styles.
⬇️ Result | Source ➡️
Scrub all styles from source:
<div >No styles allowed</div>
#3 Scrub HTML Strings (Remove HTML Tags)