feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
package sourceloc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-27 12:32:26 +00:00
|
|
|
|
"html"
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
"sort"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"unicode"
|
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Unit is one structural piece of an original file (a paragraph, a slide
|
|
|
|
|
|
// shape, a sheet row) with the locator it should resolve to.
|
|
|
|
|
|
type Unit struct {
|
|
|
|
|
|
Text string
|
|
|
|
|
|
Locator types.SourceLocator
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// Skip units too short to place reliably.
|
|
|
|
|
|
const alignMinRunes = 2
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// Align maps complete source units, never extending a hit through unmatched
|
|
|
|
|
|
// content. Unknown gaps must stay unknown: assigning them to the preceding
|
|
|
|
|
|
// block can point a citation at an entirely different page.
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
func Align(markdown string, units []Unit) []types.SourceBlock {
|
|
|
|
|
|
norm := normalizeMarkdown(markdown)
|
2026-09-27 12:32:26 +00:00
|
|
|
|
runes := []rune(markdown)
|
|
|
|
|
|
var blocks []types.SourceBlock
|
|
|
|
|
|
cursor, floor := 0, 0
|
|
|
|
|
|
counts := make(map[string]int)
|
|
|
|
|
|
matches := make(map[string][]int)
|
|
|
|
|
|
for _, u := range units {
|
|
|
|
|
|
counts[normalizeKey(u.Text)]++
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
for _, u := range units {
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
key := normalizeKey(u.Text)
|
|
|
|
|
|
if utf8.RuneCountInString(key) < alignMinRunes {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// Repeated source text is safe only when every occurrence is accounted
|
|
|
|
|
|
// for by a source unit in reading order (not a TOC or an omitted block).
|
|
|
|
|
|
positions, known := matches[key]
|
|
|
|
|
|
if !known {
|
|
|
|
|
|
positions = normalizedMatches(norm.text, key)
|
|
|
|
|
|
matches[key] = positions
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(positions) != counts[key] {
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
matchIndex := sort.SearchInts(positions, cursor)
|
|
|
|
|
|
if matchIndex == len(positions) {
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
at := positions[matchIndex]
|
|
|
|
|
|
endByte := at + len(key)
|
|
|
|
|
|
start := lineLeadStart(runes, norm.pos[at], floor)
|
|
|
|
|
|
end := norm.end[endByte-1]
|
|
|
|
|
|
// Include trailing markup/URLs on the matched line and blank lines,
|
|
|
|
|
|
// but stop before the next visible text's line. This also preserves
|
|
|
|
|
|
// an image's destination without swallowing another paragraph.
|
|
|
|
|
|
if endByte == len(norm.text) {
|
|
|
|
|
|
end = len(runes)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
next := norm.pos[endByte]
|
|
|
|
|
|
lead := next
|
|
|
|
|
|
for lead > 0 && runes[lead-1] != '\n' {
|
|
|
|
|
|
lead--
|
|
|
|
|
|
}
|
|
|
|
|
|
if lead >= end {
|
|
|
|
|
|
end = lead
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
loc := u.Locator
|
|
|
|
|
|
loc.Mapping = "exact"
|
|
|
|
|
|
blocks = append(blocks, types.SourceBlock{Start: start, End: end, Locator: loc})
|
|
|
|
|
|
cursor, floor = endByte, end
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
return blocks
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// normalizedMatches rejects a number embedded in a different numeric value.
|
|
|
|
|
|
func normalizedMatches(text, key string) []int {
|
|
|
|
|
|
var out []int
|
|
|
|
|
|
if key == "" {
|
|
|
|
|
|
return out
|
|
|
|
|
|
}
|
|
|
|
|
|
first, _ := utf8.DecodeRuneInString(key)
|
|
|
|
|
|
last, _ := utf8.DecodeLastRuneInString(key)
|
|
|
|
|
|
for from := 0; from <= len(text)-len(key); {
|
|
|
|
|
|
i := strings.Index(text[from:], key)
|
|
|
|
|
|
if i < 0 {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
at := from + i
|
|
|
|
|
|
before, _ := utf8.DecodeLastRuneInString(text[:at])
|
|
|
|
|
|
after, _ := utf8.DecodeRuneInString(text[at+len(key):])
|
|
|
|
|
|
left := unicode.IsDigit(first) && (unicode.IsDigit(before) || strings.ContainsRune(".,:/+−-", before))
|
|
|
|
|
|
right := unicode.IsDigit(last) && (unicode.IsDigit(after) || strings.ContainsRune(".,:/%‰", after))
|
|
|
|
|
|
if !left && !right {
|
|
|
|
|
|
out = append(out, at)
|
|
|
|
|
|
}
|
|
|
|
|
|
_, size := utf8.DecodeRuneInString(text[at:])
|
|
|
|
|
|
from = at + size
|
|
|
|
|
|
}
|
|
|
|
|
|
return out
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
// lineLeadStart moves pos back to the start of its line when everything
|
|
|
|
|
|
// before it on the line is markup or numbering ("## ", "| ", "1. ", "(2) "),
|
|
|
|
|
|
// which belongs to the unit but was not part of its text. It never crosses
|
|
|
|
|
|
// floor.
|
|
|
|
|
|
func lineLeadStart(runes []rune, pos, floor int) int {
|
|
|
|
|
|
const maxLead = 16
|
|
|
|
|
|
start := pos
|
|
|
|
|
|
for start > floor && runes[start-1] != '\n' {
|
|
|
|
|
|
if pos-start >= maxLead || unicode.IsLetter(runes[start-1]) {
|
|
|
|
|
|
return pos
|
|
|
|
|
|
}
|
|
|
|
|
|
start--
|
|
|
|
|
|
}
|
|
|
|
|
|
return start
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// normalized retains letters, digits and numeric symbols: text holds the
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
// kept runes, pos[b] the rune offset in the source of the rune starting at
|
|
|
|
|
|
// byte b of text.
|
|
|
|
|
|
type normalized struct {
|
|
|
|
|
|
text string
|
|
|
|
|
|
pos []int
|
2026-09-27 12:32:26 +00:00
|
|
|
|
end []int
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// normalizeMarkdown projects markdown onto its letters and digits, skipping
|
|
|
|
|
|
// link and image destinations and HTML tags, whose URLs and attributes are
|
|
|
|
|
|
// not text of the document.
|
|
|
|
|
|
func normalizeMarkdown(markdown string) normalized {
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
|
|
b.Grow(len(markdown))
|
|
|
|
|
|
pos := make([]int, 0, len(markdown))
|
2026-09-27 12:32:26 +00:00
|
|
|
|
ends := make([]int, 0, len(markdown))
|
|
|
|
|
|
emit := func(r rune, start, end int) {
|
|
|
|
|
|
before := b.Len()
|
|
|
|
|
|
b.WriteRune(r)
|
|
|
|
|
|
for k := before; k < b.Len(); k++ {
|
|
|
|
|
|
pos = append(pos, start)
|
|
|
|
|
|
ends = append(ends, end)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
runes := []rune(markdown)
|
|
|
|
|
|
for i := 0; i < len(runes); i++ {
|
|
|
|
|
|
r := runes[i]
|
|
|
|
|
|
switch {
|
|
|
|
|
|
case r == '<' && i+1 < len(runes) && (isASCIILetter(runes[i+1]) || runes[i+1] == '/' || runes[i+1] == '!'):
|
|
|
|
|
|
if end := indexRune(runes, '>', i+1, 4096); end > 0 {
|
|
|
|
|
|
i = end
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
case r == ']' && i+1 < len(runes) && runes[i+1] == '(':
|
|
|
|
|
|
if end := indexRune(runes, ')', i+2, 1<<20); end > 0 {
|
|
|
|
|
|
i = end
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
if r == '&' {
|
|
|
|
|
|
if end := indexRune(runes, ';', i+1, 32); end > 0 {
|
|
|
|
|
|
raw := string(runes[i : end+1])
|
|
|
|
|
|
if decoded := html.UnescapeString(raw); decoded != raw {
|
|
|
|
|
|
context := []rune{' '}
|
|
|
|
|
|
if i > 0 {
|
|
|
|
|
|
context[0] = runes[i-1]
|
|
|
|
|
|
}
|
|
|
|
|
|
context = append(context, []rune(decoded)...)
|
|
|
|
|
|
context = append(context, ' ')
|
|
|
|
|
|
if end+1 < len(runes) {
|
|
|
|
|
|
context[len(context)-1] = runes[end+1]
|
|
|
|
|
|
}
|
|
|
|
|
|
for j := 1; j+1 < len(context); j++ {
|
|
|
|
|
|
if nr, ok := matchRune(context, j); ok {
|
|
|
|
|
|
emit(nr, i, end+1)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
i = end
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
if nr, ok := matchRune(runes, i); ok {
|
|
|
|
|
|
emit(nr, i, i+1)
|
|
|
|
|
|
}
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
2026-09-27 12:32:26 +00:00
|
|
|
|
return normalized{text: b.String(), pos: pos, end: ends}
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// normalizeKey projects plain text onto its letters and digits.
|
|
|
|
|
|
func normalizeKey(text string) string {
|
|
|
|
|
|
var b strings.Builder
|
2026-09-27 12:32:26 +00:00
|
|
|
|
runes := []rune(text)
|
|
|
|
|
|
for i := range runes {
|
|
|
|
|
|
if r, ok := matchRune(runes, i); ok {
|
|
|
|
|
|
b.WriteRune(r)
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return b.String()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-27 12:32:26 +00:00
|
|
|
|
// Preserve distinctions such as 1.5/15, -5/5, 5%/5 and <5/>5.
|
|
|
|
|
|
func matchRune(rs []rune, i int) (rune, bool) {
|
|
|
|
|
|
r := rs[i]
|
|
|
|
|
|
if r >= 0xFF01 && r <= 0xFF5E {
|
|
|
|
|
|
r -= 0xFEE0
|
|
|
|
|
|
}
|
|
|
|
|
|
if !strings.ContainsRune(".,:/+−-%‰<>=≤≥≠", r) {
|
|
|
|
|
|
return foldRune(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
before, after := i-1, i+1
|
|
|
|
|
|
for before >= 0 && (rs[before] == ' ' || rs[before] == '\t') {
|
|
|
|
|
|
before--
|
|
|
|
|
|
}
|
|
|
|
|
|
for after < len(rs) && (rs[after] == ' ' || rs[after] == '\t') {
|
|
|
|
|
|
after++
|
|
|
|
|
|
}
|
|
|
|
|
|
prevDigit := before >= 0 && unicode.IsDigit(rs[before])
|
|
|
|
|
|
nextDigit := after < len(rs) && unicode.IsDigit(rs[after])
|
|
|
|
|
|
if ((r == '.' || r == ',' || r == '/' || r == ':') && prevDigit && nextDigit) ||
|
|
|
|
|
|
((r == '-' || r == '+' || r == '−') && nextDigit) ||
|
|
|
|
|
|
((r == '%' || r == '‰') && prevDigit) || strings.ContainsRune("<>=≤≥≠", r) {
|
|
|
|
|
|
if r == '−' {
|
|
|
|
|
|
r = '-'
|
|
|
|
|
|
}
|
|
|
|
|
|
return r, true
|
|
|
|
|
|
}
|
|
|
|
|
|
return foldRune(r)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
|
// foldRune keeps letters and digits, lower-cased and with full-width ASCII
|
|
|
|
|
|
// folded to its half-width form.
|
|
|
|
|
|
func foldRune(r rune) (rune, bool) {
|
|
|
|
|
|
if r >= 0xFF01 && r <= 0xFF5E {
|
|
|
|
|
|
r -= 0xFEE0
|
|
|
|
|
|
}
|
|
|
|
|
|
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
|
|
|
|
|
|
return 0, false
|
|
|
|
|
|
}
|
|
|
|
|
|
return unicode.ToLower(r), true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isASCIILetter(r rune) bool {
|
|
|
|
|
|
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// indexRune finds r in runes[from:], giving up after limit runes or at a
|
|
|
|
|
|
// blank line.
|
|
|
|
|
|
func indexRune(runes []rune, r rune, from, limit int) int {
|
|
|
|
|
|
end := min(len(runes), from+limit)
|
|
|
|
|
|
for i := from; i < end; i++ {
|
|
|
|
|
|
if runes[i] == r {
|
|
|
|
|
|
return i
|
|
|
|
|
|
}
|
|
|
|
|
|
if runes[i] == '\n' && i+1 < len(runes) && runes[i+1] == '\n' {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SortBlocks orders blocks by start offset.
|
|
|
|
|
|
func SortBlocks(blocks []types.SourceBlock) {
|
|
|
|
|
|
sort.SliceStable(blocks, func(i, j int) bool { return blocks[i].Start < blocks[j].Start })
|
|
|
|
|
|
}
|