Skip to content

Commit

Permalink
Sort highlights in LTR reading order
Browse files Browse the repository at this point in the history
  • Loading branch information
interstateone committed Nov 17, 2018
1 parent 5d92d97 commit 1a6cb98
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/highlights-to-markdown/PDFKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ extension PDFPage {
return annotations.filter { $0.type == "Highlight" }
}
}

func topLeftMost(_ first: PDFAnnotation, _ second: PDFAnnotation) -> Bool {
return first.bounds.origin.x < second.bounds.origin.x || first.bounds.origin.y > second.bounds.origin.y
}
2 changes: 1 addition & 1 deletion Sources/highlights-to-markdown/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let url = URL(fileURLWithPath: CommandLine.arguments[1])
let pdf = PDFDocument(url: url)!

let highlights = pdf.pages.enumerated().flatMap { pageIndex, page in
return page.highlights.map { annotation -> Highlight in
return page.highlights.sorted(by: topLeftMost).map { annotation -> Highlight in
return Highlight(pageIndex: pageIndex,
text: (annotation.quadrilateralPoints ?? [])
.map { annotation.bounds.origin + $0.point }
Expand Down

0 comments on commit 1a6cb98

Please sign in to comment.