Skip to content

nit: precompile rule title #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/formats/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
// EmptyYAMLValue represents an empty YAML value that renders as just a space after the colon
type EmptyYAMLValue struct{}

// Pre-compiled regex for extracting rule titles
var titleRegex = regexp.MustCompile(`(?m)^#\s+(.+)$`)

func (e EmptyYAMLValue) MarshalYAML() (interface{}, error) {
var node yaml.Node
node.Kind = yaml.ScalarNode
Expand Down Expand Up @@ -373,8 +376,7 @@ func GetRuleName(filePath string, sourceDir string) (string, error) {
// ExtractRuleTitle extracts the title from the content of a rule file
func ExtractRuleTitle(content []byte) string {
// Look for a markdown heading (# Title)
re := regexp.MustCompile(`(?m)^#\s+(.+)$`)
match := re.FindSubmatch(content)
match := titleRegex.FindSubmatch(content)

if len(match) > 1 {
return string(match[1])
Expand Down