Skip to content

Commit 4ed6db6

Browse files
vitlindanicolasfara
authored andcommitted
fix: link in markdown table
1 parent 079e0df commit 4ed6db6

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/main/scala/dev/atedeg/HtmlParsing.scala

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@ import net.ruippeixotog.scalascraper.dsl.DSL.*
77

88
object HtmlParsing {
99

10-
def extractColumn(document: Browser#DocumentType, columnConfig: ColumnConfig): Either[String, String] =
10+
def extractColumn(
11+
document: Browser#DocumentType,
12+
fileName: String,
13+
columnConfig: ColumnConfig,
14+
): Either[String, String] = {
1115
(document >?> element(columnConfig.htmlTag))
1216
.map(_.childNodes)
13-
.map(toMarkdown)
17+
.map(toMarkdown(_, fileName))
1418
.toRight(s"Cannot extract column ${columnConfig.name}")
19+
}
1520

16-
private def toMarkdown(es: Iterable[Node]): String = {
21+
private def toMarkdown(es: Iterable[Node], fileName: String): String = {
1722
es.foldLeft("") { (acc, elem) =>
1823
elem match {
1924
case TextNode(s) => acc + s
20-
case ElementNode(e) if isLink(e) => acc + toMarkdownLink(e)
21-
case ElementNode(e) => acc + toMarkdown(e.childNodes)
25+
case ElementNode(e) if isLink(e) => acc + toMarkdownLink(e, fileName)
26+
case ElementNode(e) => acc + toMarkdown(e.childNodes, fileName)
2227
}
2328
}
2429
}
2530

2631
private def isLink(e: Element): Boolean = e.tagName == "a"
27-
private def toMarkdownLink(e: Element): String = s"[${e.text}](${e.attr("href")})"
32+
private def toMarkdownLink(e: Element, fileName: String): String = s"[$fileName](${e.text})"
2833

2934
}

src/main/scala/dev/atedeg/Table.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ object Table {
3535

3636
private def parseFile(file: File, columnConfigs: List[ColumnConfig]): Either[String, Row] = {
3737
val document = JsoupBrowser() parseFile file.toJava
38-
columnConfigs.map(HtmlParsing.extractColumn(document, _)).sequence.map(Row)
38+
columnConfigs.map(HtmlParsing.extractColumn(document, file.nameWithoutExtension, _)).sequence.map(Row)
3939
}
4040
}

0 commit comments

Comments
 (0)