feat: add support for page layout within navigation #8
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Posts (and keeps updated) a sticky comment linking each docs PR to its live preview. | |
| # | |
| # `pull_request_target` so the workflow also runs for fork PRs — safe here because it never checks | |
| # out or executes PR code; it only reads PR metadata and writes a comment. Previews for fork PRs | |
| # stay disabled until a maintainer adds the `preview:enabled` label (enforced server-side too, see | |
| # `server/utils/github.ts`), so the comment either shows the links or explains how to enable them. | |
| name: preview comment | |
| on: | |
| pull_request_target: | |
| branches: | |
| - main | |
| paths: | |
| - 'playground/content/**' | |
| types: | |
| - opened | |
| - reopened | |
| - synchronize | |
| - labeled | |
| - unlabeled | |
| permissions: | |
| pull-requests: write | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | |
| jobs: | |
| comment: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create or update preview comment | |
| uses: actions/github-script@v8 | |
| env: | |
| SITE_URL: https://docs-template.comark.dev | |
| CONTENT_DIR: playground/content | |
| PREVIEW_LABEL: preview:enabled | |
| with: | |
| script: | | |
| const marker = '<!-- comark-docs-preview -->' | |
| const { SITE_URL, CONTENT_DIR, PREVIEW_LABEL } = process.env | |
| const { owner, repo } = context.repo | |
| const pr = context.payload.pull_request | |
| const internal = pr.head.repo && pr.head.repo.full_name === `${owner}/${repo}` | |
| const enabled = internal || pr.labels.some((label) => label.name === PREVIEW_LABEL) | |
| let body | |
| if (!enabled) { | |
| body = [ | |
| marker, | |
| '## Documentation previews', | |
| '', | |
| 'Previews are disabled for pull requests from forks.', | |
| `A maintainer can add the \`${PREVIEW_LABEL}\` label to enable them.`, | |
| ].join('\n') | |
| } else { | |
| const previewRoot = `${SITE_URL}/pr/${pr.number}` | |
| const files = await github.paginate(github.rest.pulls.listFiles, { | |
| owner, | |
| repo, | |
| pull_number: pr.number, | |
| per_page: 100, | |
| }) | |
| const prefix = `${CONTENT_DIR}/` | |
| const pages = files | |
| .filter((file) => | |
| file.status !== 'removed' | |
| && file.filename.startsWith(prefix) | |
| && file.filename.endsWith('.md'), | |
| ) | |
| .map((file) => { | |
| const relativePath = file.filename | |
| .slice(prefix.length) | |
| .replace(/\.md$/, '') | |
| const routeSegments = relativePath | |
| .split('/') | |
| .map((segment) => segment.replace(/^\d+\./, '')) | |
| .map(encodeURIComponent) | |
| if (routeSegments.at(-1) === 'index') { | |
| routeSegments.pop() | |
| } | |
| const route = routeSegments.join('/') | |
| return { | |
| filename: file.filename, | |
| route, | |
| url: route ? `${previewRoot}/${route}` : `${previewRoot}/`, | |
| } | |
| }) | |
| .sort((a, b) => a.filename.localeCompare(b.filename)) | |
| body = [ | |
| marker, | |
| '## Documentation previews', | |
| '', | |
| `📚 [Preview all documentation changes](${previewRoot}) (follows new pushes)`, | |
| ...(pages.length | |
| ? [ | |
| '', | |
| ...pages.map((page) => `- [/${page.route}](${page.url})`), | |
| ] | |
| : []), | |
| '', | |
| `Pinned to the current head: [\`${pr.head.sha.slice(0, 7)}\`](${SITE_URL}/blob/${pr.head.sha})`, | |
| ...(internal | |
| ? [] | |
| : ['', `Enabled by the \`${PREVIEW_LABEL}\` label — remove it to disable the preview.`]), | |
| ].join('\n') | |
| } | |
| const comments = await github.paginate(github.rest.issues.listComments, { | |
| owner, | |
| repo, | |
| issue_number: pr.number, | |
| per_page: 100, | |
| }) | |
| const existingComment = comments.find((comment) => | |
| comment.user?.type === 'Bot' && comment.body?.includes(marker), | |
| ) | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner, | |
| repo, | |
| comment_id: existingComment.id, | |
| body, | |
| }) | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number: pr.number, | |
| body, | |
| }) | |
| } |