#2 Use Scrub to Remove All or Specific HTML Tags
RazorBlade IScrub.All(...), IScrub.Only(), IScrub.Except()
These demos show how to strip all html from a string. You often need this in combination with crop or ellipsis to create teasers.
Simple example
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<
Get IScrub service
@{
var scrubSvc = GetService<IScrub>();
}
After IScrub.All
Introduction Welcome to this blog post beginning with xyz we'll tell you more
<code>@scrubSvc.All(val1)</code>
Usually you will then combine with crop or ellipsis
Introduction Welcome to this blog post beginning…
<code>@Html.Raw(Text.Ellipsis(scrubSvc.All(val1), cropLen))</code>
IScrub.Only()
Only scrub specified tags using IScrub.Only(source, tag)
Scrub one tag: <h2><span>The cool title</span></h2>
Scrub multiple tags: <span>The cool title</span>
@{
var exampleTagsOnly = "<div><h2><span>The cool title</span></h2></div>";
}
<div><strong>Scrub one tag: </strong><code>@scrubSvc.Only(exampleTagsOnly, "div")</code></div>
<div><strong>Scrub multiple tags: </strong><code>@scrubSvc.Only(exampleTagsOnly, new string[] { "div", "h2" })</code></div>
IScrub.Except()
Scrub except the specified tags using IScrub.Except(source, tag)
Scrub tags except one tag: <h2>The cool title</h2>
Scrub tags except multiple tags: <div><span>The cool title</span></div>
@{
var exampleTagsExcept = "<div><h2><span>The cool title</span></h2></div>";
}
<div><strong>Scrub tags except one tag: </strong><code>@scrubSvc.Except(exampleTagsExcept, "h2")</code></div>
<div><strong>Scrub tags except multiple tags: </strong><code>@scrubSvc.Except(exampleTagsExcept, new string[] { "div", "span" })</code></div>
#2 Use Scrub to Remove All or Specific HTML Tags
@inherits Custom.Hybrid.Razor12
@using ToSic.Razor.Blade;
@using ToSic.Sxc.Services;
<!-- unimportant stuff, hidden -->
<div class="row">
<div class="col-md-7">
RazorBlade IScrub.All(...)... <!-- unimportant stuff, hidden -->
</div>
<div class="col-md-5">
@Html.Partial("../shared/_InfoSection.cshtml", new { Data = viewMd.Specs.Requirements, Title = "Requirements", Icon = "fa-exclamation-circle" })
@Html.Partial("../shared/_InfoSection.cshtml", new { Data = viewMd.Specs.Resources, Title = "Resources" })
</div>
</div>
@{
var val1 = "<h1>Introduction</h1><p>Welcome to this blog post</p><hr><p>beginning with xyz we'll tell you more</p>";
var cropLen = 50;
}
<h2>Simple example</h2>
<h3>Original</h3>
<code>@val1</code>
<div>which if cropped at @cropLen would be really messy</div>
<code>@val1.Substring(0, cropLen)</code>
Get IScrub service
@{
var scrubSvc = GetService<IScrub>();
}
<h3>After IScrub.All</h3>
<code>@scrubSvc.All(val1)</code>
<h3>Usually you will then combine with crop or ellipsis</h3>
<code>@Html.Raw(Text.Ellipsis(scrubSvc.All(val1), cropLen))</code>
<h3>IScrub.Only()</h3>
<p>Only scrub specified tags using <code>IScrub.Only(source, tag)</code></p>
@{
var exampleTagsOnly = "<div><h2><span>The cool title</span></h2></div>";
}
<div><strong>Scrub one tag: </strong><code>@scrubSvc.Only(exampleTagsOnly, "div")</code></div>
<div><strong>Scrub multiple tags: </strong><code>@scrubSvc.Only(exampleTagsOnly, new string[] { "div", "h2" })</code></div>
<h3>IScrub.Except()</h3>
<p>Scrub except the specified tags using <code>IScrub.Except(source, tag)</code></p>
@{
var exampleTagsExcept = "<div><h2><span>The cool title</span></h2></div>";
}
<div><strong>Scrub tags except one tag: </strong><code>@scrubSvc.Except(exampleTagsExcept, "h2")</code></div>
<div><strong>Scrub tags except multiple tags: </strong><code>@scrubSvc.Except(exampleTagsExcept, new string[] { "div", "span" })</code></div>
<!-- unimportant stuff, hidden -->