Skip to content

Commit 910bb55

Browse files
authored
Merge branch 'main' into feature/fix-inline-code-in-left-navigation
2 parents 9131b38 + 62f580a commit 910bb55

File tree

13 files changed

+541
-17
lines changed

13 files changed

+541
-17
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: AWS Auth
2+
3+
description: |
4+
This is an opinionated action to authenticate with AWS.
5+
It will generate a role ARN based on the repository name and the AWS account ID.
6+
7+
inputs:
8+
aws_account_id:
9+
description: 'The AWS account ID to generate the role ARN for'
10+
required: true
11+
default: '197730964718' # elastic-web
12+
aws_region:
13+
description: 'The AWS region to use'
14+
required: false
15+
default: 'us-east-1'
16+
aws_role_name_prefix:
17+
description: 'The prefix for the role name'
18+
required: false
19+
default: 'elastic-docs-v3-preview-'
20+
21+
runs:
22+
using: composite
23+
steps:
24+
- name: Generate AWS Role ARN
25+
id: role_arn
26+
shell: python
27+
env:
28+
AWS_ACCOUNT_ID: ${{ inputs.aws_account_id }}
29+
ROLE_NAME_PREFIX: ${{ inputs.aws_role_name_prefix }}
30+
run: |
31+
import hashlib
32+
import os
33+
prefix = os.environ["ROLE_NAME_PREFIX"]
34+
m = hashlib.sha256()
35+
m.update(os.environ["GITHUB_REPOSITORY"].encode('utf-8'))
36+
hash = m.hexdigest()[:64-len(prefix)]
37+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
38+
f.write(f"result=arn:aws:iam::{os.environ["AWS_ACCOUNT_ID"]}:role/{prefix}{hash}")
39+
- name: Configure AWS Credentials
40+
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
41+
with:
42+
role-to-assume: ${{ steps.role_arn.outputs.result }}
43+
aws-region: ${{ inputs.aws_region }}

.github/workflows/pr.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
permissions:
77
contents: read
88
packages: read
9+
id-token: write
10+
pull-requests: write
11+
deployments: write
912

1013
concurrency:
1114
group: ${{ github.workflow }}-${{ github.ref }}
@@ -32,8 +35,14 @@ jobs:
3235

3336
- name: Publish AOT
3437
run: ./build.sh publishbinaries
35-
36-
# we run our artifact directly please use the prebuild
37-
# elastic/docs-builder@main GitHub Action for all other repositories!
38-
- name: Build documentation
39-
run: .artifacts/publish/docs-builder/release/docs-builder --strict
38+
39+
- uses: actions/upload-artifact@v4
40+
with:
41+
name: docs-builder-binary
42+
path: .artifacts/publish/docs-builder/release/docs-builder
43+
if-no-files-found: error
44+
retention-days: 1
45+
46+
preview:
47+
needs: build
48+
uses: ./.github/workflows/preview.yml
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: preview-cleanup
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
deployments: write
9+
id-token: write
10+
11+
jobs:
12+
cleanup:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: ./.github/actions/aws-auth
17+
- name: Delete s3 objects
18+
env:
19+
PR_NUMBER: ${{ github.event.pull_request.number }}
20+
run: |
21+
aws s3 rm "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --recursive
22+
23+
- name: Delete GitHub environment
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
const { owner, repo } = context.repo;
28+
const deployments = await github.rest.repos.listDeployments({
29+
owner,
30+
repo,
31+
environment: `preview-${context.issue.number}`
32+
});
33+
for (const deployment of deployments.data) {
34+
await github.rest.repos.createDeploymentStatus({
35+
owner,
36+
repo,
37+
deployment_id: deployment.id,
38+
state: 'inactive',
39+
description: 'Marking deployment as inactive'
40+
});
41+
await github.rest.repos.deleteDeployment({
42+
owner,
43+
repo,
44+
deployment_id: deployment.id
45+
});
46+
}
47+
48+
49+

.github/workflows/preview.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: preview
2+
3+
on:
4+
workflow_call: ~
5+
6+
permissions:
7+
id-token: write
8+
pull-requests: write
9+
deployments: write
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Create Deployment
16+
uses: actions/github-script@v7
17+
id: deployment
18+
with:
19+
result-encoding: string
20+
script: |
21+
const { owner, repo } = context.repo;
22+
const deployment = await github.rest.repos.createDeployment({
23+
issue_number: context.issue.number,
24+
owner,
25+
repo,
26+
ref: context.payload.pull_request.head.ref,
27+
environment: `preview-${context.issue.number}`,
28+
description: `Preview deployment for PR ${context.issue.number}`,
29+
auto_merge: false,
30+
required_contexts: [],
31+
})
32+
await github.rest.repos.createDeploymentStatus({
33+
deployment_id: deployment.data.id,
34+
owner,
35+
repo,
36+
state: "in_progress",
37+
description: "Deployment created",
38+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
39+
})
40+
return deployment.data.id
41+
42+
- uses: actions/checkout@v4
43+
44+
- uses: actions/download-artifact@v4
45+
with:
46+
name: docs-builder-binary
47+
48+
# we run our artifact directly please use the prebuild
49+
# elastic/docs-builder@main GitHub Action for all other repositories!
50+
- name: Build documentation
51+
env:
52+
PR_NUMBER: ${{ github.event.pull_request.number }}
53+
run: |
54+
chmod +x ./docs-builder
55+
./docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
56+
57+
- uses: ./.github/actions/aws-auth
58+
59+
- name: Upload to S3
60+
env:
61+
PR_NUMBER: ${{ github.event.pull_request.number }}
62+
run: |
63+
aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
64+
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
65+
66+
- name: Update deployment status
67+
uses: actions/github-script@v7
68+
if: steps.deployment.outputs.result
69+
with:
70+
script: |
71+
await github.rest.repos.createDeploymentStatus({
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
deployment_id: ${{ steps.deployment.outputs.result }},
75+
state: "success",
76+
description: "Deployment completed",
77+
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`,
78+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
79+
})
80+
81+
- name: Update Deployment Status on Failure
82+
if: failure() && steps.deployment.outputs.result
83+
uses: actions/github-script@v7
84+
with:
85+
script: |
86+
await github.rest.repos.createDeploymentStatus({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
deployment_id: ${{ steps.deployment.outputs.result }},
90+
state: "failure",
91+
description: "Deployment failed",
92+
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
93+
})

docs/syntax/links.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,15 @@ Do note that these inline anchors will be normalized.
9191
## This Is A Header [What about this for an anchor!]
9292
```
9393

94-
Will result in the anchor `what-about-this-for-an-anchor`.
94+
Will result in the anchor `what-about-this-for-an-anchor`.
95+
96+
97+
## Inline anchors
98+
99+
Docsbuilder temporary supports the abbility to create a linkable anchor anywhere on any document.
100+
101+
```markdown
102+
This is text and $$$this-is-an-inline-anchor$$$
103+
```
104+
105+
This feature exists to aid with migration however is scheduled for removal and new content should **NOT** utilize this feature.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Slugify;
6+
7+
namespace Elastic.Markdown.Helpers;
8+
9+
public static class SlugExtensions
10+
{
11+
private static readonly SlugHelper _slugHelper = new();
12+
13+
14+
public static string Slugify(this string? text) => _slugHelper.GenerateSlug(text);
15+
16+
}

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
using Elastic.Markdown.Myst;
99
using Elastic.Markdown.Myst.Directives;
1010
using Elastic.Markdown.Myst.FrontMatter;
11+
using Elastic.Markdown.Myst.InlineParsers;
1112
using Elastic.Markdown.Slices;
1213
using Markdig;
1314
using Markdig.Extensions.Yaml;
1415
using Markdig.Syntax;
15-
using Slugify;
1616

1717
namespace Elastic.Markdown.IO;
1818

1919

2020
public record MarkdownFile : DocumentationFile
2121
{
22-
private readonly SlugHelper _slugHelper = new();
2322
private string? _navigationTitle;
2423

2524
public MarkdownFile(IFileInfo sourceFile, IDirectoryInfo rootPath, MarkdownParser parser, BuildContext context)
@@ -162,16 +161,14 @@ private void ReadDocumentInstructions(MarkdownDocument document)
162161
Collector.EmitWarning(FilePath, "Document has no title, using file name as title.");
163162
}
164163

165-
166-
167164
var contents = document
168165
.Where(block => block is HeadingBlock { Level: >= 2 })
169166
.Cast<HeadingBlock>()
170167
.Select(h => (h.GetData("header") as string, h.GetData("anchor") as string))
171168
.Select(h => new PageTocItem
172169
{
173170
Heading = h.Item1!.StripMarkdown(),
174-
Slug = _slugHelper.GenerateSlug(h.Item2 ?? h.Item1)
171+
Slug = (h.Item2 ?? h.Item1).Slugify()
175172
})
176173
.ToList();
177174
_tableOfContent.Clear();
@@ -181,8 +178,10 @@ private void ReadDocumentInstructions(MarkdownDocument document)
181178
var labels = document.Descendants<DirectiveBlock>()
182179
.Select(b => b.CrossReferenceName)
183180
.Where(l => !string.IsNullOrWhiteSpace(l))
184-
.Select(_slugHelper.GenerateSlug)
181+
.Select(s => s.Slugify())
182+
.Concat(document.Descendants<InlineAnchor>().Select(a => a.Anchor))
185183
.ToArray();
184+
186185
foreach (var label in labels)
187186
{
188187
if (!string.IsNullOrEmpty(label))

src/Elastic.Markdown/Myst/InlineParsers/HeadingBlockWithSlugParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public override bool Close(BlockProcessor processor, Block block)
5151

5252
var newSlice = new StringSlice(header.ToString());
5353
headerBlock.Lines.Lines[0] = new StringLine(ref newSlice);
54+
55+
if (header.IndexOf('$') >= 0)
56+
anchor = HeadingAnchorParser.MatchAnchor().Replace(anchor.ToString(), "");
57+
5458
headerBlock.SetData("anchor", anchor.ToString());
5559
headerBlock.SetData("header", header.ToString());
5660
return base.Close(processor, block);
@@ -67,4 +71,7 @@ public static partial class HeadingAnchorParser
6771

6872
[GeneratedRegex(@"(?:\[[^[]+\])\s*$", RegexOptions.IgnoreCase, "en-US")]
6973
public static partial Regex MatchAnchor();
74+
75+
[GeneratedRegex(@"\$\$\$[^\$]+\$\$\$", RegexOptions.IgnoreCase, "en-US")]
76+
public static partial Regex InlineAnchors();
7077
}

0 commit comments

Comments
 (0)