Skip to content

Commit 29e5d00

Browse files
authored
Merge branch 'main' into feature/extensionless-urls
2 parents 5a8694d + 47c7663 commit 29e5d00

File tree

11 files changed

+245
-125
lines changed

11 files changed

+245
-125
lines changed

.github/workflows/preview-build.yml

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
- reopened
99
- labeled
1010
- unlabeled
11+
push:
12+
branches:
13+
- main
1114
workflow_call:
1215
inputs:
1316
strict:
@@ -38,42 +41,67 @@ jobs:
3841
steps:
3942

4043
- name: Get changed files
44+
if: github.event_name == 'pull_request'
4145
id: check-files
4246
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
4347
with:
4448
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
4549

4650
- name: Checkout
47-
if: steps.check-files.outputs.any_changed == 'true'
51+
if: github.event_name != 'pull_request' || steps.check-files.outputs.any_changed == 'true'
4852
uses: actions/checkout@v4
4953
with:
5054
persist-credentials: false
5155

52-
- name: Store PR data
56+
- name: Generate path prefix
5357
env:
5458
PR_NUMBER: ${{ github.event.pull_request.number }}
55-
PR_REF: ${{ github.event.pull_request.head.sha }}
56-
ANY_CHANGED: ${{ steps.check-files.outputs.any_changed }}
5759
run: |
58-
cat << EOF > pull_request.json
60+
case "${GITHUB_EVENT_NAME}" in
61+
"pull_request")
62+
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_ENV
63+
;;
64+
"push")
65+
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
66+
if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
67+
echo "Unsupported ref name: ${GITHUB_REF_NAME}";
68+
exit 1;
69+
fi
70+
;;
71+
*)
72+
echo "Unsupported event: ${GITHUB_EVENT_NAME}";
73+
exit 1;
74+
;;
75+
esac
76+
77+
- name: Store deployment metadata
78+
id: metadata
79+
env:
80+
PR_NUMBER: ${{ github.event.pull_request.number }}
81+
REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
82+
SHOULD_DEPLOY: ${{ github.event_name == 'pull_request' && steps.check-files.outputs.any_changed || 'true' }}
83+
run: |
84+
cat << EOF > deployment_metadata.json
5985
{
60-
"number": ${PR_NUMBER},
61-
"ref": "${PR_REF}",
62-
"any_changed": ${ANY_CHANGED}
86+
"pr_number": "${PR_NUMBER}",
87+
"ref": "${REF}",
88+
"should_deploy": "${SHOULD_DEPLOY}",
89+
"path_prefix": "${PATH_PREFIX}"
6390
}
6491
EOF
92+
echo "should_deploy=${SHOULD_DEPLOY}" >> "${GITHUB_OUTPUT}"
6593
66-
- name: Upload PR data
94+
- name: Upload deployment metadata
6795
uses: actions/upload-artifact@v4
6896
with:
69-
name: pull-request-data
70-
path: pull_request.json
97+
name: deployment_metadata
98+
path: deployment_metadata.json
7199
if-no-files-found: error
72100
retention-days: 1
73101
compression-level: 1
74102

75103
- name: Bootstrap Action Workspace
76-
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
104+
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
77105
uses: ./.github/actions/bootstrap
78106

79107
- name: Set REDESIGN feature flag
@@ -83,22 +111,20 @@ jobs:
83111
# we run our artifact directly please use the prebuild
84112
# elastic/docs-builder@main GitHub Action for all other repositories!
85113
- name: Build documentation
86-
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
87-
env:
88-
PR_NUMBER: ${{ github.event.pull_request.number }}
114+
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
89115
run: |
90-
dotnet run --project src/docs-builder -- --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
116+
dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
91117
92118
- name: Build documentation
93-
if: github.repository != 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
119+
if: github.repository != 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
94120
uses: elastic/docs-builder@main
95121
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
96122
with:
97-
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
123+
prefix: ${{ env.PATH_PREFIX }}
98124
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
99125

100126
- uses: actions/upload-artifact@v4
101-
if: steps.check-files.outputs.any_changed == 'true'
127+
if: steps.metadata.outputs.should_deploy == 'true'
102128
with:
103129
name: docs
104130
path: .artifacts/docs/html/

.github/workflows/preview-deploy.yml

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,57 @@ permissions:
1414
actions: read
1515

1616
jobs:
17-
pull-request-data:
17+
deployment-metadata:
1818
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1919
runs-on: ubuntu-latest
2020
outputs:
21-
number: ${{ steps.pull_request.outputs.number }}
22-
ref: ${{ steps.pull_request.outputs.ref }}
23-
any_changed: ${{ steps.pull_request.outputs.any_changed }}
21+
pr_number: ${{ steps.metadata.outputs.pr_number }}
22+
ref: ${{ steps.metadata.outputs.ref }}
23+
should_deploy: ${{ steps.metadata.outputs.should_deploy }}
24+
path_prefix: ${{ steps.metadata.outputs.path_prefix }}
2425
steps:
25-
- name: Download PR data
26+
- name: Download deployment metadata
2627
env:
2728
GH_TOKEN: ${{ github.token }}
2829
run: |
2930
gh run download ${{ github.event.workflow_run.id }} \
3031
--repo "${GITHUB_REPOSITORY}" \
31-
--name pull-request-data
32-
- name: Get PR data
33-
id: pull_request
32+
--name deployment_metadata
33+
- name: Get deployment metadata
34+
id: metadata
3435
run: |
3536
{
36-
echo "number=$(jq -r '.number' pull_request.json)"
37-
echo "ref=$(jq -r '.ref' pull_request.json)"
38-
echo "any_changed=$(jq -r '.any_changed' pull_request.json)"
37+
echo "pr_number=$(jq -r '.pr_number' deployment_metadata.json)"
38+
echo "ref=$(jq -r '.ref' deployment_metadata.json)"
39+
echo "path_prefix=$(jq -r '.path_prefix' deployment_metadata.json)"
40+
echo "should_deploy=$(jq -r '.should_deploy' deployment_metadata.json)"
3941
} >> "${GITHUB_OUTPUT}"
4042
4143
deploy:
42-
needs: pull-request-data
43-
if: needs.pull-request-data.outputs.any_changed == 'true'
44+
needs: deployment-metadata
45+
if: needs.deployment-metadata.outputs.should_deploy == 'true'
4446
runs-on: ubuntu-latest
4547
concurrency:
46-
group: ${{ github.workflow }}-${{ needs.pull-request-data.outputs.number }}
48+
group: ${{ github.workflow }}-${{ needs.deployment-metadata.outputs.pr_number }}
4749
cancel-in-progress: true
4850
steps:
4951
- name: Create Deployment
5052
uses: actions/github-script@v7
5153
id: deployment
5254
env:
53-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
54-
PR_REF: ${{ needs.pull-request-data.outputs.ref }}
55+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
56+
REF: ${{ needs.deployment-metadata.outputs.ref }}
5557
with:
5658
result-encoding: string
5759
script: |
5860
const { owner, repo } = context.repo;
61+
const prNumber = process.env.PR_NUMBER;
62+
const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview';
5963
const deployment = await github.rest.repos.createDeployment({
6064
owner,
6165
repo,
62-
ref: process.env.PR_REF,
63-
environment: `docs-preview-${process.env.PR_NUMBER}`,
66+
environment,
67+
ref: process.env.REF,
6468
auto_merge: false,
6569
required_contexts: [],
6670
})
@@ -86,25 +90,27 @@ jobs:
8690

8791
- name: Upload to S3
8892
env:
89-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
93+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
94+
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
9095
run: |
91-
aws s3 sync ./html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
96+
aws s3 sync ./html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
9297
aws cloudfront create-invalidation \
9398
--distribution-id EKT7LT5PM8RKS \
94-
--paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
99+
--paths "${PATH_PREFIX}" "/${PATH_PREFIX}/*"
95100
96101
- name: Update deployment status
97102
uses: actions/github-script@v7
98103
if: always() && steps.deployment.outputs.result
99104
env:
100-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
105+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
106+
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
101107
with:
102108
script: |
103109
await github.rest.repos.createDeploymentStatus({
104110
owner: context.repo.owner,
105111
repo: context.repo.repo,
106112
deployment_id: ${{ steps.deployment.outputs.result }},
107113
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
108-
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}`,
114+
environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
109115
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
110116
})

docs/migration/guide/how-to-set-up-docs-previews.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@ This way you only build and deploy the docs when there are changes to the docs a
2525
name: docs-build <1>
2626

2727
on:
28+
push: <2>
29+
branches:
30+
- main
2831
pull_request: ~
2932

3033
jobs:
3134
docs-preview:
32-
uses: elastic/docs-builder/.github/workflows/preview-build.yml <2>
35+
uses: elastic/docs-builder/.github/workflows/preview-build.yml <3>
3336
with:
34-
path-pattern: docs/** <3>
37+
path-pattern: docs/** <4>
3538
permissions:
3639
contents: read
3740
pull-requests: read
3841
```
3942
4043
1. The naming is important so that the `docs-deploy` workflow is triggered.
41-
2. This should be the path to your docs folder.
44+
2. You can omit the `push` event if you only want to build the docs on PRs.
4245
3. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml)
46+
4. This should be the path to your docs folder.
4347

4448

4549
::::

docs/syntax/_snippets/reusable-snippet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is a snippet included on "{{page_title}}".
1+
This is a snippet included on "{{context.page_title}}".
22

33
:::{tip}
44
This is a snippet

docs/testing/nested/index.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
# Testing Nesting
1+
---
2+
sub:
3+
x: "Variable"
4+
navigation_title: "Testing nesting and {{x}}"
5+
---
6+
# Testing Nesting and {{x}}
27

3-
The files in this directory are used for testing purposes. Do not edit these files unless you are working on tests.
8+
The files in this directory are used for testing purposes. Do not edit these files unless you are working on tests.
9+
10+
11+
## Injecting a {{x}} is supported in headers.
12+
13+
This should show up in the file's table of contents too.

src/Elastic.Markdown/Helpers/Interpolation.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Diagnostics.CodeAnalysis;
56
using System.Text.RegularExpressions;
7+
using Elastic.Markdown.Myst;
68

79
namespace Elastic.Markdown.Helpers;
810

@@ -14,18 +16,60 @@ internal static partial class InterpolationRegex
1416

1517
public static class Interpolation
1618
{
17-
public static bool ReplaceSubstitutions(this ReadOnlySpan<char> span, Dictionary<string, string>? properties, out string? replacement)
19+
public static string ReplaceSubstitutions(
20+
this string input,
21+
ParserContext context
22+
)
23+
{
24+
var span = input.AsSpan();
25+
if (span.ReplaceSubstitutions([context.Substitutions, context.ContextSubstitutions], out var replacement))
26+
return replacement;
27+
return input;
28+
}
29+
30+
31+
public static bool ReplaceSubstitutions(
32+
this ReadOnlySpan<char> span,
33+
ParserContext context,
34+
[NotNullWhen(true)] out string? replacement
35+
) =>
36+
span.ReplaceSubstitutions([context.Substitutions, context.ContextSubstitutions], out replacement);
37+
38+
public static bool ReplaceSubstitutions(
39+
this ReadOnlySpan<char> span,
40+
IReadOnlyDictionary<string, string>? properties,
41+
[NotNullWhen(true)] out string? replacement
42+
)
43+
{
44+
replacement = null;
45+
if (properties is null || properties.Count == 0)
46+
return false;
47+
48+
if (span.IndexOf("}}") < 0)
49+
return false;
50+
51+
return span.ReplaceSubstitutions([properties], out replacement);
52+
}
53+
54+
public static bool ReplaceSubstitutions(
55+
this ReadOnlySpan<char> span,
56+
IReadOnlyDictionary<string, string>[] properties,
57+
[NotNullWhen(true)] out string? replacement
58+
)
1859
{
1960
replacement = null;
2061
if (span.IndexOf("}}") < 0)
2162
return false;
2263

23-
var substitutions = properties ?? new();
24-
if (substitutions.Count == 0)
64+
if (properties.Length == 0 || properties.Sum(p => p.Count) == 0)
2565
return false;
2666

67+
var lookups = properties
68+
.Select(p => p as Dictionary<string, string> ?? new Dictionary<string, string>(p, StringComparer.OrdinalIgnoreCase))
69+
.Select(d => d.GetAlternateLookup<ReadOnlySpan<char>>())
70+
.ToArray();
71+
2772
var matchSubs = InterpolationRegex.MatchSubstitutions().EnumerateMatches(span);
28-
var lookup = substitutions.GetAlternateLookup<ReadOnlySpan<char>>();
2973

3074
var replaced = false;
3175
foreach (var match in matchSubs)
@@ -35,14 +79,15 @@ public static bool ReplaceSubstitutions(this ReadOnlySpan<char> span, Dictionary
3579

3680
var spanMatch = span.Slice(match.Index, match.Length);
3781
var key = spanMatch.Trim(['{', '}']);
82+
foreach (var lookup in lookups)
83+
{
84+
if (!lookup.TryGetValue(key, out var value))
85+
continue;
3886

39-
if (!lookup.TryGetValue(key, out var value))
40-
continue;
41-
42-
replacement ??= span.ToString();
43-
replacement = replacement.Replace(spanMatch.ToString(), value);
44-
replaced = true;
45-
87+
replacement ??= span.ToString();
88+
replacement = replacement.Replace(spanMatch.ToString(), value);
89+
replaced = true;
90+
}
4691
}
4792

4893
return replaced;

0 commit comments

Comments
 (0)