// SPDX-License-Identifier: GPL-3.0-or-later package mdx import ( "regexp" "strings" ) // leadingNumber matches a heading that starts with a two digit number. var leadingNumber = regexp.MustCompile(`^(\d\d)\b`) // nonSlug matches a run of characters that cannot be part of a slug. var nonSlug = regexp.MustCompile(`[^a-z0-9]+`) // Slug turns a heading into the identifier used for its anchor. A heading that // starts with a two digit number gets that number ("01. Title" becomes "01"), // the preamble, commitments and epilogue headings of the manifesto get the same // identifier in every language, and any other heading becomes lowercase words // joined by hyphens. func Slug(heading string) string { if m := leadingNumber.FindStringSubmatch(heading); m != nil { return m[1] } lower := strings.ToLower(strings.TrimSpace(heading)) for _, group := range wellKnown { for _, prefix := range group.prefixes { if strings.HasPrefix(lower, prefix) { return group.slug } } } return strings.Trim(nonSlug.ReplaceAllString(lower, "-"), "-") } // wellKnown lists the heading prefixes of the manifesto, in all its languages, // and the identifier each one gets. var wellKnown = []struct { slug string prefixes []string }{ {"preamble", []string{"preamble", "preambolo", "preámbulo", "präambel", "préambule"}}, {"commitments", []string{"our commitments", "i nostri impegni", "nuestros compromisos", "unsere verpflichtungen", "nos engagements"}}, {"epilogue", []string{"epilogue", "epilogo", "epílogo", "epilog", "épilogue"}}, }