-
Notifications
You must be signed in to change notification settings - Fork 5
ci: bound Discord issue announcement formatting #183
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfb2523
ci: preserve Markdown in Discord issue announcements
Daxiongmao87 d4a8ba4
ci: address Discord Markdown review feedback
Daxiongmao87 3192847
ci: preserve complex Markdown during truncation
Daxiongmao87 a335c00
ci: harden Markdown delimiter parsing
Daxiongmao87 2f94b84
ci: preserve literal Markdown delimiters
Daxiongmao87 faa0965
ci: validate Markdown delimiter roles
Daxiongmao87 d2c1936
ci: finish Discord Markdown review fixes
Daxiongmao87 b77a126
ci: harden Discord Markdown parsing (Fixes #181)
Daxiongmao87 8169413
Merge remote-tracking branch 'origin/main' into fix/discord-issue-mar…
Daxiongmao87 63e6137
ci: address remaining Markdown review (Fixes #181)
Daxiongmao87 be31b92
ci: address follow-up Markdown review (Fixes #181)
Daxiongmao87 114a480
ci: simplify Discord issue announcements (Fixes #181)
Daxiongmao87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
| export const DESCRIPTION_LIMIT = 700; | ||
| export const TITLE_LIMIT = 256; | ||
|
|
||
| const EMPTY_DESCRIPTION = '_No description provided._'; | ||
| const ELLIPSIS = '...'; | ||
| const BLOCK_BUDGET = DESCRIPTION_LIMIT - ELLIPSIS.length - 2; | ||
|
|
||
| function normalizeLineEndings(content) { | ||
| return content.replace(/\r\n?/gu, '\n'); | ||
| } | ||
|
|
||
| function codePointLength(content) { | ||
| const iterator = content[Symbol.iterator](); | ||
| let length = 0; | ||
| while (!iterator.next().done) length += 1; | ||
| return length; | ||
| } | ||
|
|
||
| function codePointSlice(content, limit) { | ||
| let end = 0; | ||
| for (const character of content) { | ||
| if (limit === 0) break; | ||
| end += character.length; | ||
| limit -= 1; | ||
| } | ||
| return content.slice(0, end); | ||
| } | ||
|
|
||
| function fenceOpening(line) { | ||
| const match = line.match(/^ {0,3}(`{3,})(.*)$/u); | ||
| if (!match || match[2].includes('`')) return null; | ||
|
|
||
| return { length: match[1].length }; | ||
| } | ||
|
|
||
| function closesFence(line, fence) { | ||
| const match = line.match(/^ {0,3}(`{3,})[ \t]*$/u); | ||
| return Boolean(match && match[1].length >= fence.length); | ||
| } | ||
|
|
||
| function isIndentedCode(line) { | ||
| return /^(?:(?: {0,3}>[ \t]?)+)?(?: {0,3}\t| {4})/u.test(line); | ||
| } | ||
|
|
||
| function headingLabel(line) { | ||
| const indent = line.match(/^[ \t]*/u)?.[0] ?? ''; | ||
| if (indent.length > 3) return null; | ||
|
|
||
| const match = line.slice(indent.length).match(/^#{1,6}[ \t]+(.+)$/u); | ||
| if (!match) return null; | ||
|
|
||
| const label = match[1].replace(/[ \t]+#+[ \t]*$/u, '').trim(); | ||
| return label ? `${indent}**${label}**` : null; | ||
| } | ||
|
|
||
| function normalizeProseLine(line) { | ||
| if (/^[ \t]*$/u.test(line)) return ''; | ||
| if (isIndentedCode(line)) return line; | ||
|
|
||
| const indent = line.match(/^[ \t]*/u)?.[0] ?? ''; | ||
| const content = line | ||
| .slice(indent.length) | ||
| .replace(/[ \t]+/gu, ' ') | ||
|
Daxiongmao87 marked this conversation as resolved.
|
||
| .trimEnd(); | ||
| return indent + content; | ||
| } | ||
|
|
||
| function appendNormalizedProse(output, headingLines, line) { | ||
| const heading = isIndentedCode(line) ? null : headingLabel(line); | ||
| const normalized = heading ?? normalizeProseLine(line); | ||
| if (!normalized && (output.length === 0 || output.at(-1) === '')) return; | ||
| if (heading) headingLines.add(output.length); | ||
| output.push(normalized); | ||
| } | ||
|
|
||
| function normalizedMarkdown(body) { | ||
| const lines = normalizeLineEndings(body).split('\n'); | ||
| const output = []; | ||
| const headingLines = new Set(); | ||
| let fence = null; | ||
|
|
||
| for (const line of lines) { | ||
| if (fence) { | ||
| output.push(line); | ||
| if (closesFence(line, fence)) fence = null; | ||
| continue; | ||
| } | ||
|
|
||
| const opening = fenceOpening(line); | ||
| if (opening) { | ||
| output.push(line); | ||
|
Daxiongmao87 marked this conversation as resolved.
|
||
| fence = opening; | ||
| continue; | ||
| } | ||
|
|
||
| appendNormalizedProse(output, headingLines, line); | ||
| } | ||
|
|
||
| while (output.at(-1) === '') output.pop(); | ||
| return { | ||
| content: output.join('\n'), | ||
| headingLines, | ||
| }; | ||
| } | ||
|
|
||
| function markdownBlocks(normalized) { | ||
| const blocks = []; | ||
| let current = []; | ||
| let currentIsHeading = false; | ||
| let fence = null; | ||
|
|
||
| const flush = kind => { | ||
| if (current.length === 0) return; | ||
| blocks.push({ | ||
| kind: kind ?? (currentIsHeading ? 'heading' : 'prose'), | ||
| text: current.join('\n'), | ||
| }); | ||
| current = []; | ||
| currentIsHeading = false; | ||
| }; | ||
|
|
||
| for (const [index, line] of normalized.content.split('\n').entries()) { | ||
| if (fence) { | ||
| current.push(line); | ||
| if (closesFence(line, fence)) { | ||
| fence = null; | ||
| flush('fence'); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (line === '') { | ||
| flush(); | ||
| continue; | ||
| } | ||
|
|
||
| const opening = fenceOpening(line); | ||
| if (opening) { | ||
| flush(); | ||
| current.push(line); | ||
| fence = opening; | ||
| continue; | ||
| } | ||
|
|
||
| currentIsHeading = current.length === 0 && normalized.headingLines.has(index); | ||
| current.push(line); | ||
|
Daxiongmao87 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| flush(fence ? 'fence' : undefined); | ||
| return blocks; | ||
| } | ||
|
|
||
| function wordPrefix(content, limit) { | ||
| const slice = codePointSlice(content, limit).trimEnd(); | ||
| const boundary = Math.max(slice.lastIndexOf(' '), slice.lastIndexOf('\n')); | ||
| return (boundary > 0 ? slice.slice(0, boundary) : slice).trimEnd(); | ||
| } | ||
|
|
||
| function truncatedDescription(normalized) { | ||
| const blocks = markdownBlocks(normalized); | ||
| const selected = []; | ||
|
|
||
| for (const block of blocks) { | ||
| const candidate = [...selected, block].map(item => item.text).join('\n\n'); | ||
| if (codePointLength(candidate) > BLOCK_BUDGET) break; | ||
| selected.push(block); | ||
| } | ||
|
|
||
| while (selected.at(-1)?.kind === 'heading') selected.pop(); | ||
| if (selected.length > 0) { | ||
| return `${selected.map(block => block.text).join('\n\n')}\n\n${ELLIPSIS}`; | ||
| } | ||
|
|
||
| const fallback = blocks.find(block => block.kind !== 'heading'); | ||
| if (!fallback || fallback.kind !== 'prose') return ELLIPSIS; | ||
| return `${wordPrefix(fallback.text, DESCRIPTION_LIMIT - ELLIPSIS.length)}${ELLIPSIS}`; | ||
| } | ||
|
|
||
| export function formatDescription(body) { | ||
| if (body == null || String(body).trim() === '') return EMPTY_DESCRIPTION; | ||
|
|
||
| const normalized = normalizedMarkdown(String(body)); | ||
| if (!normalized.content) return EMPTY_DESCRIPTION; | ||
| return codePointLength(normalized.content) <= DESCRIPTION_LIMIT | ||
| ? normalized.content | ||
| : truncatedDescription(normalized); | ||
| } | ||
|
|
||
| function boundedTitle(title) { | ||
| const content = String(title ?? ''); | ||
| if (codePointLength(content) <= TITLE_LIMIT) return content; | ||
| return codePointSlice(content, TITLE_LIMIT - ELLIPSIS.length) + ELLIPSIS; | ||
| } | ||
|
|
||
| export function buildDiscordPayload(event, repository) { | ||
| const issue = event.issue; | ||
| return { | ||
| username: 'GitHub Issues', | ||
| allowed_mentions: { parse: [] }, | ||
| embeds: [ | ||
| { | ||
| title: boundedTitle(issue.title), | ||
| url: issue.html_url, | ||
| description: formatDescription(issue.body), | ||
| color: 5814783, | ||
| author: { | ||
| name: `Opened by @${issue.user.login}`, | ||
| url: issue.user.html_url, | ||
| icon_url: issue.user.avatar_url, | ||
| }, | ||
| fields: [ | ||
| { | ||
| name: 'Issue', | ||
| value: `[Open issue #${issue.number}](${issue.html_url})`, | ||
| inline: false, | ||
| }, | ||
| ], | ||
| footer: { text: repository }, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| function runCli() { | ||
| const eventPath = process.argv[2] || process.env.GITHUB_EVENT_PATH; | ||
| const outputPath = process.argv[3] || 'discord-payload.json'; | ||
| const repository = process.env.REPOSITORY; | ||
|
|
||
| if (!eventPath || !repository) { | ||
| process.stderr.write('GITHUB_EVENT_PATH and REPOSITORY are required\n'); | ||
| return 1; | ||
| } | ||
|
|
||
| try { | ||
| const event = JSON.parse(fs.readFileSync(eventPath, 'utf8')); | ||
| const payload = buildDiscordPayload(event, repository); | ||
| fs.writeFileSync(outputPath, JSON.stringify(payload)); | ||
| return 0; | ||
| } catch (error) { | ||
| process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| const invokedPath = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : ''; | ||
| if (import.meta.url === invokedPath) process.exitCode = runCli(); | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.