Skip to content
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

Added support for modifying links in image URLs. Fixed linking when adding extra files from outside. #32

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ This directive runs executable with arguments and put its stderr and stdout outp
if !*disableGenCodeBlocksDirectives {
opts = append(opts, mdformatter.WithCodeBlockTransformer(mdgen.NewCodeBlockTransformer()))
}

if len(*files) == 0 {
return errors.New("no files to format")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/mdformatter/testdata/formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ See those components on this diagram:

<img src="img/arch.jpg" class="img-fluid" alt="architecture overview" />

![img](img/arch.jpg)

### [Sidecar](components/sidecar.md)

Thanos integrates with existing Prometheus servers through a [Sidecar process](https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar#solution), which runs on the same machine or in the same pod as the Prometheus server.
Expand Down
2 changes: 2 additions & 0 deletions pkg/mdformatter/testdata/formatted_and_transformed.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ See those components on this diagram:

<img src="img/arch.jpg" class="img-fluid" alt="architecture overview" />

![img]($$-img/arch.jpg-testdata/not_formatted.md-$$)

### [Sidecar]($$-components/sidecar.md-testdata/not_formatted.md-$$)

Thanos integrates with existing Prometheus servers through a [Sidecar process]($$-https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar#solution-testdata/not_formatted.md-$$), which runs on the same machine or in the same pod as the Prometheus server.
Expand Down
2 changes: 2 additions & 0 deletions pkg/mdformatter/testdata/not_formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ See those components on this diagram:

<img src="img/arch.jpg" class="img-fluid" alt="architecture overview" />

![img](img/arch.jpg)

### [Sidecar](components/sidecar.md)

Thanos integrates with existing Prometheus servers through a [Sidecar process](https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar#solution), which runs on the same machine or in the same pod as the Prometheus server.
Expand Down
16 changes: 8 additions & 8 deletions pkg/mdformatter/testdata/not_formatted.md.diff
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

Thanos bases itself on vanilla [Prometheus](https://prometheus.io/) (v2.2.1+). We plan to support *all* Prometheus version beyond this version.

@@ -77,4 +75,4 @@
@@ -79,4 +77,4 @@

If you are not interested in backing up any data, the `--objstore.config-file` flag can simply be omitted.

Expand All @@ -55,7 +55,7 @@

Let's extend the Sidecar in the previous section to connect to a Prometheus server, and expose the Store API.

@@ -96,4 +94,2 @@
@@ -98,4 +96,2 @@
--grpc-address 0.0.0.0:19090 # GRPC endpoint for StoreAPI
```

Expand All @@ -71,7 +71,7 @@

#### External Labels

@@ -168,5 +164,3 @@
@@ -170,5 +166,3 @@

Go to the configured HTTP address, and you should now be able to query across all Prometheus instances and receive de-duplicated data.

Expand All @@ -90,7 +90,7 @@

```bash
thanos query \
@@ -189,4 +183,2 @@
@@ -191,4 +185,2 @@

Read more details [here](service-discovery.md).

Expand All @@ -106,7 +106,7 @@

```bash
thanos store \
@@ -207,1 +199,1 @@
@@ -209,1 +201,1 @@

The store gateway occupies small amounts of disk space for caching basic information about data in the object storage. This will rarely exceed more than a few gigabytes and is used to improve restart times. It is useful but not required to preserve it across restarts.

Expand All @@ -115,7 +115,7 @@

### [Compactor](components/compact.md)

@@ -224,8 +216,4 @@
@@ -226,8 +218,4 @@

The compactor is not in the critical path of querying or data backup. It can either be run as a periodic batch job or be left running to always compact data as soon as possible. It is recommended to provide 100-300GB of local disk space for data processing.

Expand All @@ -140,15 +140,15 @@
```$
usage: thanos rule [<flags>]

@@ -412,1 +400,0 @@
@@ -414,1 +402,0 @@

The configuration format is the following:

-[embedmd]:# (../flags/config_rule_alerting.txt yaml)
```yaml
alertmanagers:
- http_config:
@@ -438,1 +425,4 @@
@@ -440,1 +427,4 @@
timeout: 10s
api_version: v1
```
Expand Down
9 changes: 9 additions & 0 deletions pkg/mdformatter/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (t *transformer) Render(w io.Writer, source []byte, node ast.Node) error {
if err := ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
var err error
switch typedNode := n.(type) {
// TODO(bwplotka): Add support for links inside HTML.
case *ast.Link:
if !entering || t.link == nil {
return ast.WalkSkipChildren, nil
Expand All @@ -62,6 +63,14 @@ func (t *transformer) Render(w io.Writer, source []byte, node ast.Node) error {
repl := ast.NewString(dest)
repl.SetParent(n)
n.Parent().ReplaceChild(n.Parent(), n, repl)
case *ast.Image:
if !entering || t.link == nil {
return ast.WalkSkipChildren, nil
}
typedNode.Destination, err = t.link.TransformDestination(t.sourceCtx, typedNode.Destination)
if err != nil {
return ast.WalkStop, err
}
case *ast.FencedCodeBlock:
if !entering || t.cb == nil || typedNode.Info == nil {
return ast.WalkSkipChildren, nil
Expand Down
15 changes: 11 additions & 4 deletions pkg/transform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const (
type Config struct {
Version int

// InputDir is a relative path that assumes input directory for markdown files and assets.
// InputDir is a relative (to PWD) path that assumes input directory for markdown files and assets.
InputDir string `yaml:"inputDir"`
// OutputDir is a relative output directory that we expect all files to land in. Typically that can be `content` dir
// OutputDir is a relative (to PWD) output directory that we expect all files to land in. Typically that can be `content` dir
// which hugo uses as an input.
OutputDir string `yaml:"outputDir"`
// OutputStaticDir is relative output directory for all non markdown files.
// OutputStaticDir is relative (to PWD) output directory for all non markdown files.
OutputStaticDir string `yaml:"outputStaticDir"`

// ExtraInputGlobs allows to bring files from outside of input dir.
Expand All @@ -57,11 +57,18 @@ type TransformationConfig struct {
_glob glob.Glob

// Glob matches files using https://github.com/gobwas/glob.
// Glob is matched against the relative path of the file in the input directory in
// relation to the input directory. For example:
// InputDir: dir1, File found in dir1/a/b/c/file.md, the given glob will be matched
// against a/b/c/file.md.
// After first match, file is no longer matching other elements.
Glob string

// Path is an optional different path for the file to be moved.
// NOTE: All relative links will be moved accordingly.
// If not specified, file will be moved to the exact same position as in input directory.
// Use absolute path to point the absolute structure where `/` means output directory.
// If relative path is used, it will start in the directory the file is in the input directory.
// NOTE: All relative links will be moved accordingly inside such file.
Path string

// FrontMatter holds front matter transformations.
Expand Down
2 changes: 1 addition & 1 deletion pkg/transform/testdata/mdox1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ outputStaticDir: "testdata/tmp/1static"
gitIgnored: true

transformations:
- glob: "/README.md"
- glob: "README.md"
path: _index.md
frontMatter:
template: |
Expand Down
10 changes: 5 additions & 5 deletions pkg/transform/testdata/mdox2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ version: 1
inputDir: "testdata/testproj"
outputDir: "testdata/tmp/2"
extraInputGlobs:
- "../../README.md"
- "testdata/test.md"

gitIgnored: true
localLinksStyle: hugo

transformations:
- glob: "/../../../../README.md"
path: _index.md
- glob: "../test.md"
path: /_index.md
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
Expand All @@ -20,13 +20,13 @@ transformations:
_target:
path: "/**"

- glob: "/doc.md"
- glob: "doc.md"
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
slug: "{{ .Target.FileName }}"

- glob: "/Team/doc.md"
- glob: "Team/doc.md"
path: inner/doc.md
frontMatter:
template: |
Expand Down
11 changes: 11 additions & 0 deletions pkg/transform/testdata/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test

Yolo

[RelLink](testproj/Proposals/README.md)

[RelLink](testproj/Team/doc.md)

![Image](testproj/logo.png)

![Image](testproj/images/logo2.png)
4 changes: 3 additions & 1 deletion pkg/transform/testdata/testproj/Proposals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

[RelLink](../README.md)

[RelLink](../Team/doc.md)
[RelLink](../Team/doc.md)

This should not work: [RelLink](../../test.md)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ Yolo

[RelLink](Team/doc.md)

![Image](logo.png)
![Image](logo.png)

![Image](images/logo2.png)
Binary file added pkg/transform/testdata/testproj/images/logo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading