beads/internal/ado/richtext.go
beads/crew/emma 39ab50d447 feat(ado): add Azure DevOps work item tracker package
Add the internal/ado package implementing tracker.IssueTracker for Azure
DevOps. Provides bidirectional work item sync via the ADO REST API.

Core components:
- types.go: ADO API types, SecretString with redaction, field constants
- client.go: HTTP client with Basic auth, retry, WIQL query builder
- fieldmapper.go: configurable status/type/priority mapping
- mapping.go: bidirectional IssueToBeads/IssueToTracker conversion
- richtext.go: HTML<>Markdown conversion (goldmark + bluemonday)
- tracker.go: tracker.IssueTracker implementation with init() registration
- bootstrap.go: 4-tier deduplication for first sync
- reconcile.go: deletion detection via periodic reconciliation scan
- links.go: bidirectional link sync with direction normalization

Includes full test suite (218 tests, 92.8% coverage).

Fixes applied during fix-merge:
- errors.New() → fmt.Errorf() to match project conventions (6 instances)
- Added gosec nolint for configured API endpoint SSRF false positive

Co-Authored-By: Harry Miller <harry_miller@trimble.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 14:34:28 -07:00

57 lines
1.4 KiB
Go

package ado
import (
"bytes"
"strings"
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html"
)
// HTMLToMarkdown converts ADO HTML description to Markdown for beads storage.
// It sanitizes the HTML first via bluemonday to strip dangerous elements
// (script tags, event handlers), then converts clean HTML to Markdown.
// Returns empty string for empty input.
func HTMLToMarkdown(rawHTML string) (string, error) {
if strings.TrimSpace(rawHTML) == "" {
return "", nil
}
p := bluemonday.UGCPolicy()
sanitized := p.Sanitize(rawHTML)
if strings.TrimSpace(sanitized) == "" {
return "", nil
}
md, err := htmltomarkdown.ConvertString(sanitized)
if err != nil {
return "", err
}
return strings.TrimRight(md, " \t\r\n"), nil
}
// MarkdownToHTML converts beads Markdown description to HTML for ADO storage.
// Uses goldmark with safe renderer settings (no raw HTML passthrough).
// Returns empty string for empty input.
func MarkdownToHTML(md string) (string, error) {
if strings.TrimSpace(md) == "" {
return "", nil
}
renderer := goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
),
)
var buf bytes.Buffer
if err := renderer.Convert([]byte(md), &buf); err != nil {
return "", err
}
return strings.TrimRight(buf.String(), " \t\r\n"), nil
}