Skip to content

Keep fragment/anchor in href value of <a> tags after parsing #17

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenXmlPowerTools/OpenXmlPowerTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Drawing.Common" Version="4.5.0" />
</ItemGroup>
</Project>
</Project>
22 changes: 15 additions & 7 deletions OpenXmlPowerTools/WmlToHtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Text;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using FontFamily = System.Drawing.FontFamily;

// 200e lrm - LTR
// 200f rlm - RTL
Expand Down Expand Up @@ -405,19 +407,25 @@ private static object ConvertToHtmlTransform(WordprocessingDocument wordDoc,
{
return ProcessParagraph(wordDoc, settings, element, suppressTrailingWhiteSpace, currentMarginLeft);
}

// Transform hyperlinks to the XHTML h:a element.
if (element.Name == W.hyperlink && element.Attribute(R.id) != null)
{
try
{
var a = new XElement(Xhtml.a,
new XAttribute("href",
wordDoc.MainDocumentPart
var preFragmentUrl = wordDoc.MainDocumentPart
.HyperlinkRelationships
.First(x => x.Id == (string)element.Attribute(R.id))
.Uri
),
.Uri.AbsoluteUri;
var anchor = wordDoc.MainDocumentPart.Document
.Descendants<Hyperlink>()
.First(x => x.Id == (string)element.Attribute(R.id))
.Anchor;
var fullUrl = preFragmentUrl;
if (anchor != null)
fullUrl += "#" + anchor;
var a = new XElement(Xhtml.a,
new XAttribute("href",fullUrl),
element.Elements(W.r).Select(run => ConvertRun(wordDoc, settings, run))
);
if (!a.Nodes().Any())
Expand Down Expand Up @@ -2972,7 +2980,7 @@ private static object ConvertContentThatCanContainFields(WordprocessingDocument

var content = g.DescendantsAndSelf(W.r).Select(run => ConvertRun(wordDoc, settings, run));
var a = parsed.Arguments.Length > 0
? new XElement(Xhtml.a, new XAttribute("href", parsed.Arguments[0]), content)
? new XElement(Xhtml.a, new XAttribute("href", string.Join("#", parsed.Arguments)), content)
: new XElement(Xhtml.a, content);
var a2 = a as XElement;
if (!a2.Nodes().Any())
Expand Down
Binary file modified TestFiles/HC023-Hyperlink.docx
Binary file not shown.