Skip to content

Commit 8c5ea28

Browse files
authored
Merge pull request #40 from isdaniel/feature/support-mutiple-hyperlink
Support multiple hyperlink
2 parents 3ae759f + 1d5937d commit 8c5ea28

2 files changed

Lines changed: 139 additions & 5 deletions

File tree

src/MiniWord/MiniWord.Implment.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,9 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
205205
}
206206
t.Remove();
207207
}
208-
else if (tag.Value is MiniWordHyperLink)
208+
else if (IsHyperLink(tag.Value))
209209
{
210-
var mainPart = docx.MainDocumentPart;
211-
var linkInfo = (MiniWordHyperLink)tag.Value;
212-
var hyperlink = GetHyperLink(mainPart, linkInfo);
213-
run.Append(hyperlink);
210+
AddHyperLink(docx, run, tag.Value);
214211
t.Remove();
215212
}
216213
else if (tag.Value is MiniWordColorText)
@@ -263,6 +260,34 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
263260
}
264261
}
265262

263+
private static bool IsHyperLink(object value)
264+
{
265+
return value is MiniWordHyperLink ||
266+
value is IEnumerable<MiniWordHyperLink>;
267+
}
268+
269+
private static void AddHyperLink(WordprocessingDocument docx, Run run, object value)
270+
{
271+
List<MiniWordHyperLink> links = new List<MiniWordHyperLink>();
272+
273+
if (value is MiniWordHyperLink)
274+
{
275+
links.Add((MiniWordHyperLink)value);
276+
}
277+
else
278+
{
279+
links.AddRange((IEnumerable<MiniWordHyperLink>)value);
280+
}
281+
282+
foreach (var linkInfo in links)
283+
{
284+
var mainPart = docx.MainDocumentPart;
285+
var hyperlink = GetHyperLink(mainPart, linkInfo);
286+
run.Append(hyperlink);
287+
run.Append(new Break());
288+
}
289+
}
290+
266291
private static Hyperlink GetHyperLink(MainDocumentPart mainPart, MiniWordHyperLink linkInfo)
267292
{
268293
var hr = mainPart.AddHyperlinkRelationship(new Uri(linkInfo.Url), true);

tests/MiniWordTests/IssueTests.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,115 @@ public void TestIssue5()
575575
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", xml);
576576
}
577577

578+
[Fact]
579+
public void MiniWordHyperLink_List()
580+
{
581+
var path = PathHelper.GetTempFilePath();
582+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
583+
var value = new Dictionary<string, object>()
584+
{
585+
["Name"] = new List<MiniWordHyperLink>(){
586+
new MiniWordHyperLink(){
587+
Url = "https://google.com",
588+
Text = "測試連結22!!"
589+
},
590+
new MiniWordHyperLink(){
591+
Url = "https://google1.com",
592+
Text = "測試連結11!!"
593+
}
594+
},
595+
["Company_Name"] = "MiniSofteware",
596+
["CreateDate"] = new DateTime(2021, 01, 01),
597+
["VIP"] = true,
598+
["Points"] = 123,
599+
["APP"] = "Demo APP",
600+
};
601+
MiniWord.SaveAsByTemplate(path, templatePath, value);
602+
//Console.WriteLine(path);
603+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
604+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
605+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
606+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
607+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
608+
609+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
610+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
611+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
612+
}
613+
614+
615+
[Fact]
616+
public void MiniWordHyperLink_Array()
617+
{
618+
var path = PathHelper.GetTempFilePath();
619+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
620+
var value = new Dictionary<string, object>()
621+
{
622+
["Name"] = new MiniWordHyperLink[]{
623+
new MiniWordHyperLink(){
624+
Url = "https://google.com",
625+
Text = "測試連結22!!"
626+
},
627+
new MiniWordHyperLink(){
628+
Url = "https://google1.com",
629+
Text = "測試連結11!!"
630+
}
631+
},
632+
["Company_Name"] = "MiniSofteware",
633+
["CreateDate"] = new DateTime(2021, 01, 01),
634+
["VIP"] = true,
635+
["Points"] = 123,
636+
["APP"] = "Demo APP",
637+
};
638+
MiniWord.SaveAsByTemplate(path, templatePath, value);
639+
640+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
641+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
642+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
643+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
644+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
645+
646+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
647+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
648+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
649+
}
650+
651+
[Fact]
652+
public void MiniWordHyperLink_AnonymousArray()
653+
{
654+
var path = PathHelper.GetTempFilePath();
655+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
656+
var value = new Dictionary<string, object>()
657+
{
658+
["Name"] = new []{
659+
new MiniWordHyperLink(){
660+
Url = "https://google.com",
661+
Text = "測試連結22!!"
662+
},
663+
new MiniWordHyperLink(){
664+
Url = "https://google1.com",
665+
Text = "測試連結11!!"
666+
}
667+
},
668+
["Company_Name"] = "MiniSofteware",
669+
["CreateDate"] = new DateTime(2021, 01, 01),
670+
["VIP"] = true,
671+
["Points"] = 123,
672+
["APP"] = "Demo APP",
673+
};
674+
MiniWord.SaveAsByTemplate(path, templatePath, value);
675+
676+
var docXml = Helpers.GetZipFileContent(path, "word/document.xml");
677+
Assert.DoesNotContain("Jack Demo APP Account Data", docXml);
678+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
679+
Assert.Contains("MiniSofteware Demo APP Account Data", docXml);
680+
Assert.Contains("<w:hyperlink w:tgtFrame=\"_blank\"", docXml);
681+
682+
var relsXml = Helpers.GetZipFileContent(path, "word/_rels/document.xml.rels");
683+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google.com\"", relsXml);
684+
Assert.Contains("<Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink\" Target=\"https://google1.com\"", relsXml);
685+
}
686+
578687
/// <summary>
579688
/// [Fuzzy Regex replace similar key · Issue #5 · mini-software/MiniWord](https://github.com/mini-software/MiniWord/issues/5)
580689
/// </summary>

0 commit comments

Comments
 (0)