Repository: bwplotka/mdox
Pull request #45 (2021-06-15 04:55:45)

Change transform config flag to `pathorcontent`
This PR changes transform config flag to `pathorcontent`. A particular use case for this would be dynamic input and output dirs (for example when transforming docs for multiple releases of a project).


Patch:
diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml
new file mode 100644
index 0000000..b87140c
--- /dev/null
+++ b/.github/workflows/docs.yaml
@@ -0,0 +1,31 @@
+name: docs
+
+on:
+  push:
+    branches:
+      - main
+    tags:
+  pull_request:
+
+jobs:
+  check:
+    runs-on: ubuntu-latest
+    name:  Documentation check
+    env:
+      GOBIN: /tmp/.bin
+    steps:
+      - name: Checkout code into the Go module directory.
+        uses: actions/checkout@v2
+
+      - name: Install Go
+        uses: actions/setup-go@v2
+        with:
+          go-version: 1.15.x
+
+      - uses: actions/cache@v1
+        with:
+          path: ~/go/pkg/mod
+          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
+
+      - name: Check docs
+        run: make check-docs
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 3fce1b5..805b75e 100644
--- a/Makefile
+++ b/Makefile
@@ -58,6 +58,11 @@ docs: build ## Generates config snippets and doc formatting.
 	@echo ">> generating docs $(PATH)"
 	@PATH=$(GOBIN) mdox fmt -l *.md
 
+.PHONY: check-docs
+check-docs: build ## Checks docs for discrepancies in formatting and links.
+	@echo ">> checking formatting and links $(PATH)"
+	@PATH=$(GOBIN) mdox fmt --check -l *.md
+
 .PHONY: format
 format: ## Formats Go code.
 format: $(GOIMPORTS)
diff --git a/README.md b/README.md
index f1520c9..e96126b 100644
--- a/README.md
+++ b/README.md
@@ -63,12 +63,12 @@ Flags:
       --links.validate.config-file=<file-path>  
                                  Path to YAML file for skipping link check, with
                                  spec defined in
-                                 github.com/bwplotka/mdox/pkg/linktransformer.Config
+                                 github.com/bwplotka/mdox/pkg/linktransformer.ValidatorConfig
       --links.validate.config=<content>  
                                  Alternative to 'links.validate.config-file'
                                  flag (mutually exclusive). Content of YAML file
                                  for skipping link check, with spec defined in
-                                 github.com/bwplotka/mdox/pkg/linktransformer.Config
+                                 github.com/bwplotka/mdox/pkg/linktransformer.ValidatorConfig
 
 Args:
   <files>  Markdown file(s) to process.
diff --git a/main.go b/main.go
index ff90d46..811615a 100644
--- a/main.go
+++ b/main.go
@@ -127,7 +127,7 @@ This directive runs executable with arguments and put its stderr and stdout outp
 		"Absolute path links will be converted to relative links to anchor dir as well.").Regexp()
 	// TODO(bwplotka): Add cache in file?
 	linksValidateEnabled := cmd.Flag("links.validate", "If true, all links will be validated").Short('l').Bool()
-	linksValidateConfig := extflag.RegisterPathOrContent(cmd, "links.validate.config", "YAML file for skipping link check, with spec defined in github.com/bwplotka/mdox/pkg/linktransformer.Config", extflag.WithEnvSubstitution())
+	linksValidateConfig := extflag.RegisterPathOrContent(cmd, "links.validate.config", "YAML file for skipping link check, with spec defined in github.com/bwplotka/mdox/pkg/linktransformer.ValidatorConfig", extflag.WithEnvSubstitution())
 
 	cmd.Run(func(ctx context.Context, logger log.Logger) (err error) {
 		var opts []mdformatter.Option
@@ -209,9 +209,12 @@ func validateAnchorDir(anchorDir string, files []string) (_ string, err error) {
 
 func registerTransform(_ context.Context, app *extkingpin.App) {
 	cmd := app.Command("transform", "Transform markdown files in various ways. For example pre process markdown files to allow it for use for popular static HTML websites based on markdown source code and front matter options.")
-	cfg := cmd.Flag("config", "Path to the YAML file with spec defined in github.com/bwplotka/mdox/pkg/transform.Config").
-		Short('c').Default(".mdox.yaml").ExistingFile()
+	cfg := extflag.RegisterPathOrContent(cmd, "config", "Path to the YAML file with spec defined in github.com/bwplotka/mdox/pkg/transform.Config", extflag.WithEnvSubstitution())
 	cmd.Run(func(ctx context.Context, logger log.Logger) error {
-		return transform.Dir(ctx, logger, *cfg)
+		validateConfig, err := cfg.Content()
+		if err != nil {
+			return err
+		}
+		return transform.Dir(ctx, logger, validateConfig)
 	})
 }
diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go
index f992750..369b0b1 100644
--- a/pkg/mdformatter/linktransformer/link.go
+++ b/pkg/mdformatter/linktransformer/link.go
@@ -351,7 +351,7 @@ func toHeaderID(header []byte) string {
 		switch h {
 		case '{':
 			return string(id)
-		case ' ':
+		case ' ', '-':
 			id = append(id, '-')
 		default:
 		}
diff --git a/pkg/mdformatter/mdgen/mdgen.go b/pkg/mdformatter/mdgen/mdgen.go
index c8a3b62..23af687 100644
--- a/pkg/mdformatter/mdgen/mdgen.go
+++ b/pkg/mdformatter/mdgen/mdgen.go
@@ -5,7 +5,6 @@ package mdgen
 
 import (
 	"bytes"
-	"context"
 	"os/exec"
 	"strconv"
 	"strings"
@@ -17,8 +16,6 @@ import (
 )
 
 const (
-	infoStringKeyLang     = "mdox-gen-lang"
-	infoStringKeyType     = "mdox-gen-type"
 	infoStringKeyExec     = "mdox-exec"
 	infoStringKeyExitCode = "mdox-expect-exit-code"
 )
@@ -40,19 +37,22 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
 	}
 	infoStringAttr := map[string]string{}
 	for i, field := range infoFiels {
-		val := strings.Split(field, "=")
+		val := []string{field}
+		if i := strings.Index(field, "="); i != -1 {
+			val = []string{field[:i], field[i+1:]}
+		}
 		if i == 0 && len(val) == 2 {
 			return nil, errors.Errorf("missing language info in fenced code block. Got info string %q", string(infoString))
 		}
 		switch val[0] {
-		case infoStringKeyExec, infoStringKeyExitCode:
+		case infoStringKeyExec:
 			if len(val) != 2 {
-				return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value2> %q=<value2>. Got info string %q", val[0], infoStringKeyExitCode, infoStringKeyExec, string(infoString))
+				return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"<value1>\" but got %s", val[0], infoStringKeyExec, string(infoString))
 			}
 			infoStringAttr[val[0]] = val[1]
-		case infoStringKeyLang, infoStringKeyType:
+		case infoStringKeyExitCode:
 			if len(val) != 2 {
-				return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value2> %q=<value2>. Got info string %q", val[0], infoStringKeyLang, infoStringKeyType, string(infoString))
+				return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"<value1>\" but got %s", val[0], infoStringKeyExitCode, string(infoString))
 			}
 			infoStringAttr[val[0]] = val[1]
 		}
@@ -82,30 +82,12 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
 			if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == expectedCode {
 				return b.Bytes(), nil
 			}
-			return nil, errors.Wrapf(err, "run %v", execCmd)
+			return nil, errors.Wrapf(err, "run %v, out: %v", execCmd, b.String())
 		}
 		return b.Bytes(), nil
 	}
 
-	lang, langOk := infoStringAttr[infoStringKeyLang]
-	typePath, typOk := infoStringAttr[infoStringKeyType]
-	if typOk || langOk {
-		if typOk != langOk {
-			return nil, errors.Errorf("got ambiguous attributes: %v. Expected is e.g ```yaml %q=<value> %q=go . Got info string %q", infoStringAttr, infoStringKeyType, infoStringKeyLang, string(infoString))
-		}
-		switch lang {
-		case "go", "golang":
-			return genGo(ctx, "", typePath)
-		default:
-			return nil, errors.Errorf("expected language a first element of info string got %q; Got info string %q", lang, string(infoString))
-		}
-	}
 	panic("should never get here")
 }
 
 func (t *genCodeBlockTransformer) Close(ctx mdformatter.SourceContext) error { return nil }
-
-func genGo(ctx context.Context, moduleRoot string, typePath string) ([]byte, error) {
-	// TODO(bwplotka): To be done.
-	return nil, nil
-}
diff --git a/pkg/transform/config.go b/pkg/transform/config.go
index a213867..6892dac 100644
--- a/pkg/transform/config.go
+++ b/pkg/transform/config.go
@@ -5,7 +5,6 @@ package transform
 
 import (
 	"bytes"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -116,18 +115,6 @@ type FrontMatterConfig struct {
 	Template string
 }
 
-func parseConfigFile(configFile string) (Config, error) {
-	configFile, err := filepath.Abs(configFile)
-	if err != nil {
-		return Config{}, errors.Wrap(err, "abs")
-	}
-	c, err := ioutil.ReadFile(configFile)
-	if err != nil {
-		return Config{}, errors.Wrap(err, "read config file")
-	}
-	return ParseConfig(c)
-}
-
 func ParseConfig(c []byte) (Config, error) {
 	cfg := Config{}
 	dec := yaml.NewDecoder(bytes.NewReader(c))
diff --git a/pkg/transform/transform.go b/pkg/transform/transform.go
index 5450590..2871bb6 100644
--- a/pkg/transform/transform.go
+++ b/pkg/transform/transform.go
@@ -49,8 +49,8 @@ func prepOutputDir(d string, gitIgnored bool) error {
 }
 
 // Dir transforms directory using given configuration file.
-func Dir(ctx context.Context, logger log.Logger, configFile string) error {
-	c, err := parseConfigFile(configFile)
+func Dir(ctx context.Context, logger log.Logger, config []byte) error {
+	c, err := ParseConfig(config)
 	if err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/mdformatter/linktransformer/link_test.go b/pkg/mdformatter/linktransformer/link_test.go
index de8dde7..4e33a1f 100644
--- a/pkg/mdformatter/linktransformer/link_test.go
+++ b/pkg/mdformatter/linktransformer/link_test.go
@@ -181,6 +181,28 @@ func TestValidator_TransformDestination(t *testing.T) {
 		testutil.Equals(t, 0, len(diff), diff.String())
 	})
 
+	t.Run("check valid local links with dash", func(t *testing.T) {
+		testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-local-links-with-dash.md")
+		testutil.Ok(t, ioutil.WriteFile(testFile, []byte(`# Expose UI on a sub-path
+
+[1](#expose-ui-on-a-sub-path)
+
+# Run-time deduplication of HA groups
+
+[2](#run-time-deduplication-of-ha-groups)
+`), os.ModePerm))
+
+		diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile})
+		testutil.Ok(t, err)
+		testutil.Equals(t, 0, len(diff), diff.String())
+
+		diff, err = mdformatter.IsFormatted(context.TODO(), logger, []string{testFile}, mdformatter.WithLinkTransformer(
+			MustNewValidator(logger, []byte(""), anchorDir),
+		))
+		testutil.Ok(t, err)
+		testutil.Equals(t, 0, len(diff), diff.String())
+	})
+
 	t.Run("check invalid local links", func(t *testing.T) {
 		testFile := filepath.Join(tmpDir, "repo", "docs", "test", "invalid-local-links.md")
 		filePath := "/repo/docs/test/invalid-local-links.md"
diff --git a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
index a423640..90f1581 100755
--- a/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
+++ b/pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
@@ -4,10 +4,6 @@
 test output
 ```
 
-```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config"
-TO BE DONE
-```
-
 ```yaml
 abc
 sad
@@ -40,3 +36,8 @@ test output3
 
 echo "test output3"
 ```
+
+```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig"
+test output2
+newline
+```
diff --git a/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md b/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md
index 3085b70..657f37a 100644
--- a/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md
+++ b/pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md
@@ -6,10 +6,6 @@ a
 adf
 ```
 
-```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config"
-TO BE DONE
-```
-
 ```yaml
 abc
 sad
@@ -41,3 +37,6 @@ abc
 
 ```bash mdox-exec="sed -n '1,3p' ./testdata/out3.sh"
 ```
+
+```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig"
+```
diff --git a/pkg/transform/transform_e2e_test.go b/pkg/transform/transform_e2e_test.go
index 8195806..46773f1 100644
--- a/pkg/transform/transform_e2e_test.go
+++ b/pkg/transform/transform_e2e_test.go
@@ -28,11 +28,15 @@ func TestTransform(t *testing.T) {
 
 	testutil.Ok(t, os.RemoveAll(tmpDir))
 	t.Run("mdox1.yaml", func(t *testing.T) {
-		testutil.Ok(t, transform.Dir(context.Background(), logger, filepath.Join(testData, "mdox1.yaml")))
+		mdox1, err := ioutil.ReadFile(filepath.Join(testData, "mdox1.yaml"))
+		testutil.Ok(t, err)
+		testutil.Ok(t, transform.Dir(context.Background(), logger, mdox1))
 		assertDirContent(t, filepath.Join(testData, "expected", "test1"), filepath.Join(tmpDir, "test1"))
 	})
 	t.Run("mdox2.yaml", func(t *testing.T) {
-		testutil.Ok(t, transform.Dir(context.Background(), logger, filepath.Join(testData, "mdox2.yaml")))
+		mdox2, err := ioutil.ReadFile(filepath.Join(testData, "mdox2.yaml"))
+		testutil.Ok(t, err)
+		testutil.Ok(t, transform.Dir(context.Background(), logger, mdox2))
 		assertDirContent(t, filepath.Join(testData, "expected", "test2"), filepath.Join(tmpDir, "test2"))
 
 	})