Space from config and title from h1 (#153)

* Add option to define Space.
Add option to use H1 heading to define page title.

* Update readme
This commit is contained in:
klysunkin 2022-01-18 09:05:26 +03:00 committed by GitHub
parent 851a8047f3
commit 8d58ff26a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 14 deletions

View File

@ -438,7 +438,9 @@ mark -h | --help
- `-c <path>` or `--config <path>` — Specify a path to the configuration file. - `-c <path>` or `--config <path>` — Specify a path to the configuration file.
- `-k` — Lock page editing to current user only to prevent accidental - `-k` — Lock page editing to current user only to prevent accidental
manual edits over Confluence Web UI. manual edits over Confluence Web UI.
- `--space <space>` - Use specified space key. If not specified space ley must be set in a page metadata.
- `--drop-h1` Don't include H1 headings in Confluence output. - `--drop-h1` Don't include H1 headings in Confluence output.
- `--title-from-h1` - Extract page title from a leading H1 heading. If no H1 heading on a page then title must be set in a page metadata.
- `--dry-run` — Show resulting HTML and don't update Confluence page content. - `--dry-run` — Show resulting HTML and don't update Confluence page content.
- `--minor-edit` — Don't send notifications while updating Confluence page. - `--minor-edit` — Don't send notifications while updating Confluence page.
- `--trace` — Enable trace logs. - `--trace` — Enable trace logs.

24
main.go
View File

@ -24,6 +24,7 @@ type Flags struct {
DryRun bool `docopt:"--dry-run"` DryRun bool `docopt:"--dry-run"`
EditLock bool `docopt:"-k"` EditLock bool `docopt:"-k"`
DropH1 bool `docopt:"--drop-h1"` DropH1 bool `docopt:"--drop-h1"`
TitleFromH1 bool `docopt:"--title-from-h1"`
MinorEdit bool `docopt:"--minor-edit"` MinorEdit bool `docopt:"--minor-edit"`
Color string `docopt:"--color"` Color string `docopt:"--color"`
Debug bool `docopt:"--debug"` Debug bool `docopt:"--debug"`
@ -34,6 +35,7 @@ type Flags struct {
BaseURL string `docopt:"--base-url"` BaseURL string `docopt:"--base-url"`
Config string `docopt:"--config"` Config string `docopt:"--config"`
Ci bool `docopt:"--ci"` Ci bool `docopt:"--ci"`
Space string `docopt:"--space"`
} }
const ( const (
@ -61,7 +63,11 @@ Options:
Supports file globbing patterns (needs to be quoted). Supports file globbing patterns (needs to be quoted).
-k Lock page editing to current user only to prevent accidental -k Lock page editing to current user only to prevent accidental
manual edits over Confluence Web UI. manual edits over Confluence Web UI.
--space <space> Use specified space key. If not specified space ley must
be set in a page metadata.
--drop-h1 Don't include H1 headings in Confluence output. --drop-h1 Don't include H1 headings in Confluence output.
--title-from-h1 Extract page title from a leading H1 heading. If no H1 heading
on a page then title must be set in a page metadata.
--dry-run Resolve page and ancestry, show resulting HTML and exit. --dry-run Resolve page and ancestry, show resulting HTML and exit.
--compile-only Show resulting HTML and don't update Confluence page content. --compile-only Show resulting HTML and don't update Confluence page content.
--minor-edit Don't send notifications while updating Confluence page. --minor-edit Don't send notifications while updating Confluence page.
@ -170,6 +176,24 @@ func processFile(
log.Fatal(err) log.Fatal(err)
} }
switch {
case meta.Space == "" && flags.Space == "":
log.Fatal("space is not set ('Space' header is not set and '--space' option is not set)")
case meta.Space == "" && flags.Space != "":
meta.Space = flags.Space
}
if meta.Title == "" && flags.TitleFromH1 {
meta.Title = mark.ExtractDocumentLeadingH1(markdown)
}
if meta.Title == "" {
log.Fatal(
`page title is not set ('Title' header is not set ` +
`and '--title-from-h1' option is not set or there is no H1 in the file)`,
)
}
stdlib, err := stdlib.New(api) stdlib, err := stdlib.New(api)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -147,3 +147,14 @@ func DropDocumentLeadingH1(
markdown = h1.ReplaceAll(markdown, []byte("")) markdown = h1.ReplaceAll(markdown, []byte(""))
return markdown return markdown
} }
// ExtractDocumentLeadingH1 will extract leading H1 heading
func ExtractDocumentLeadingH1(markdown []byte) string {
h1 := regexp.MustCompile(`^#[^#]\s*(.*)\s*\n`)
groups := h1.FindSubmatch(markdown)
if groups == nil {
return ""
} else {
return string(groups[1])
}
}

View File

@ -48,3 +48,16 @@ func TestCompileMarkdown(t *testing.T) {
test.EqualValues(string(html), actual, filename+" vs "+htmlname) test.EqualValues(string(html), actual, filename+" vs "+htmlname)
} }
} }
func TestExtractDocumentLeadingH1(t *testing.T) {
filename := "testdata/header.md"
markdown, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
actual := ExtractDocumentLeadingH1(markdown)
assert.Equal(t, "a", actual)
}

View File

@ -128,19 +128,5 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) {
return nil, data, nil return nil, data, nil
} }
if meta.Space == "" {
return nil, nil, fmt.Errorf(
"space key is not set (%s header is not set)",
HeaderSpace,
)
}
if meta.Title == "" {
return nil, nil, fmt.Errorf(
"page title is not set (%s header is not set)",
HeaderTitle,
)
}
return meta, data[offset:], nil return meta, data[offset:], nil
} }