The Sitecore way to replace the text in a Rich Text field would be to create a custom processor in <renderField>
pipeline, insert it before RenderWebEditing
to ensure all links in fields are expanded. You would then need to wrap the text of the all <a>
elements which links to a pdf with the additonal markup you require, probably best done using something like HtmlAgilityPack which is available in a default install of Sitecore. The process is very similar to this blog post by John West about adding Title attributes to images in Rich Text fields. You can see the code in the Github repo.
If you are using Glass Mapper then you will additionally to use the ForceRenderField
setting to force the field to run through the RenderField
pipeline or create a custom Data Mapper since Glass does not use the pipeline in Normal or Preview mode.
But, it looks like you are trying to show an icon using FontAwesome on all links to pdf files. I suggest that this is much more easily achieved using CSS, the endswith attribute selector and ::before selector with content to set the icon.
HTML:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div>
<p>this is some sample text</p>
<p><a href="/-/media/document.pdf">this is a document link</a></p>
<p><a href="/some/page">this is a regular link</a></p>
</div>
CSS:
a[href$=".pdf"] {
font-weight: bold;
padding-left: 0.25em;
}
a[href$=".pdf"]:before {
content: "\f1c1";
font-family: FontAwesome;
float: left;
font-size: 1.5em;
}
Output:
You can see a Codepen demo here.
You can find the unicode character code for each icon one the FontAwesome site, for example for [file-pdf-o][10]
file-pdf-o
: