Skip to content

Commit

Permalink
remove prerendered response rest docs (github#25652)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachmari authored Mar 4, 2022
1 parent 0d8bd53 commit 9fd4fe0
Show file tree
Hide file tree
Showing 17 changed files with 31,085 additions and 31,094 deletions.
11 changes: 9 additions & 2 deletions components/article/ClientSideHighlightJS.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import hljs from 'highlight.js/lib/core'
import json from 'highlight.js/lib/languages/json'
import javascript from 'highlight.js/lib/languages/javascript'
import hljsCurl from 'highlightjs-curl'

// Add as needed. It's pretty cheap to add but please don't use
// highlight.js import that loads all and everything.
hljs.registerLanguage('json', json)
const SUPPORTED_LANGUAGES = ['json']
hljs.registerLanguage('javascript', javascript)
hljs.registerLanguage('curl', hljsCurl)
const SUPPORTED_LANGUAGES = ['json', 'javascript', 'curl']

// This is the selector we use for the first document.querySelectorAll()
// to find the containers for `<code>` tags. Because it's s dataset
Expand All @@ -24,6 +29,8 @@ const CODE_ELEMENTS_PARENT_SELECTOR = '[data-highlight]'
const CODE_SELECTOR = 'pre code'

export default function ClientSideHighlightJS() {
const { asPath } = useRouter()

useEffect(() => {
// Hi Internet Explorer!
// https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#browser_compatibility
Expand Down Expand Up @@ -59,7 +66,7 @@ export default function ClientSideHighlightJS() {
intersectionObserver.observe(element)
}
}
}, [])
}, [asPath])

return null
}
2 changes: 2 additions & 0 deletions components/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ declare module '*.scss' {
const content: Record<string, string>
export default content
}

declare module 'highlightjs-curl'
13 changes: 4 additions & 9 deletions components/rest/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import styles from './CodeBlock.module.scss'
type Props = {
verb?: string
codeBlock: string
setHTML?: boolean
highlight?: string
}

export function CodeBlock({ verb, codeBlock, setHTML = false }: Props) {
return setHTML ? (
<div
className={cx(styles.codeBlock, 'rounded')}
dangerouslySetInnerHTML={{ __html: codeBlock }}
/>
) : (
<pre className={cx(styles.methodCodeBlock, 'rounded-1 border')}>
export function CodeBlock({ verb, codeBlock, highlight }: Props) {
return (
<pre className={cx(styles.methodCodeBlock, 'rounded-1 border')} data-highlight={highlight}>
<code>
{verb && (
<span className="color-bg-accent-emphasis color-fg-on-emphasis rounded-1 text-uppercase">
Expand Down
6 changes: 3 additions & 3 deletions components/rest/RestCodeSamples.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xCodeSample } from './types'
import type { xCodeSample } from './types'
import { useTranslation } from 'components/hooks/useTranslation'
import { CodeBlock } from './CodeBlock'

Expand Down Expand Up @@ -34,8 +34,8 @@ export function RestCodeSamples({ slug, xCodeSamples }: Props) {
sampleElements.push(
<CodeBlock
key={sample.lang + index}
codeBlock={sample.sourceHTML}
setHTML={true}
codeBlock={sample.source}
highlight={sample.lang === 'JavaScript' ? 'javascript' : 'curl'}
></CodeBlock>
)
}
Expand Down
2 changes: 1 addition & 1 deletion components/rest/RestResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function RestResponse({ responses }: Props) {
<CodeBlock
codeBlock={`Status: ${response.httpStatusCode} ${response.httpStatusMessage}`}
/>
<CodeBlock codeBlock={response.payload} setHTML={true} />
{response.payload ? <CodeBlock codeBlock={response.payload} highlight="json" /> : null}
</div>
)
})}
Expand Down
2 changes: 1 addition & 1 deletion components/rest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface CodeResponse {

export interface xCodeSample {
lang: string
sourceHTML: string
source: string
}

export interface Preview {
Expand Down
9,838 changes: 4,919 additions & 4,919 deletions lib/rest/static/decorated/api.github.com.json

Large diffs are not rendered by default.

8,418 changes: 4,209 additions & 4,209 deletions lib/rest/static/decorated/ghes-3.0.json

Large diffs are not rendered by default.

8,662 changes: 4,331 additions & 4,331 deletions lib/rest/static/decorated/ghes-3.1.json

Large diffs are not rendered by default.

9,726 changes: 4,863 additions & 4,863 deletions lib/rest/static/decorated/ghes-3.2.json

Large diffs are not rendered by default.

8,926 changes: 4,463 additions & 4,463 deletions lib/rest/static/decorated/ghes-3.3.json

Large diffs are not rendered by default.

8,838 changes: 4,419 additions & 4,419 deletions lib/rest/static/decorated/ghes-3.4.json

Large diffs are not rendered by default.

7,676 changes: 3,838 additions & 3,838 deletions lib/rest/static/decorated/github.ae.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"hastscript": "^7.0.2",
"helmet": "^4.6.0",
"highlight.js": "11.2.0",
"highlightjs-curl": "^1.3.0",
"highlightjs-graphql": "^1.0.2",
"hot-shots": "^9.0.0",
"html-entities": "^2.3.2",
Expand Down
27 changes: 1 addition & 26 deletions script/rest/utils/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export default class Operation {

await Promise.all([
this.renderDescription(),
this.renderCodeSamples(),
this.renderResponses(),
this.renderParameterDescriptions(),
this.renderBodyParameterDescriptions(),
Expand All @@ -100,19 +99,6 @@ export default class Operation {
return this
}

async renderCodeSamples() {
return Promise.all(
this['x-codeSamples'].map(async (sample) => {
const markdown = createCodeBlock(sample.source, sample.lang.toLowerCase())
sample.sourceHTML = await renderContent(markdown)

// Removing since we don't need this in the decorated files
delete sample.source
return sample
})
)
}

async renderResponses() {
// clone and delete this.responses so we can turn it into a clean array of objects
const rawResponses = JSON.parse(JSON.stringify(this.responses))
Expand Down Expand Up @@ -187,9 +173,7 @@ export default class Operation {
? exampleSummary || responseDescription
: exampleSummary || sentenceCase(exampleKey)

const payloadMarkdown = createCodeBlock(exampleValue, 'json')
cleanResponse.payload = await renderContent(payloadMarkdown)

cleanResponse.payload = JSON.stringify(exampleValue, null, 2)
cleanResponses.push(cleanResponse)
}
return cleanResponses
Expand Down Expand Up @@ -411,12 +395,3 @@ async function getChildParamsGroup(param) {
params: childParams,
}
}

function createCodeBlock(input, language) {
// stringify JSON if needed
if (language === 'json' && typeof input !== 'string') {
input = JSON.stringify(input, null, 2)
}

return ['```' + language, input, '```'].join('\n')
}
20 changes: 10 additions & 10 deletions tests/unit/openapi-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ describe('x-codeSamples for curl', () => {
test('GET', () => {
const operation = findOperation('GET', '/repos/{owner}/{repo}')
expect(isPlainObject(operation)).toBe(true)
const { sourceHTML } = operation['x-codeSamples'].find((sample) => sample.lang === 'Shell')
const { source } = operation['x-codeSamples'].find((sample) => sample.lang === 'Shell')
const expected =
'<pre><code class="hljs language-shell">curl \\\n' +
'curl \\\n' +
' -H "Accept: application/vnd.github.v3+json" \\\n' +
' https://api.github.com/repos/octocat/hello-world</code></pre>'
expect(sourceHTML).toEqual(expected)
' https://api.github.com/repos/octocat/hello-world'
expect(source).toEqual(expected)
})

test('operations with required preview headers match Shell examples', () => {
Expand Down Expand Up @@ -92,8 +92,8 @@ describe('x-codeSamples for @octokit/core.js', () => {
test('GET', () => {
const operation = findOperation('GET', '/repos/{owner}/{repo}')
expect(isPlainObject(operation)).toBe(true)
const { sourceHTML } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = sourceHTML.replace(/<[^>]+>/g, '').trim()
const { source } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = source.replace(/<[^>]+>/g, '').trim()
const expected = dedent`await octokit.request('GET /repos/{owner}/{repo}', {
owner: 'octocat',
repo: 'hello-world'
Expand All @@ -104,8 +104,8 @@ describe('x-codeSamples for @octokit/core.js', () => {
test('POST', () => {
const operation = findOperation('POST', '/repos/{owner}/{repo}/git/trees')
expect(isPlainObject(operation)).toBe(true)
const { sourceHTML } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = sourceHTML.replace(/<[^>]+>/g, '').trim()
const { source } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = source.replace(/<[^>]+>/g, '').trim()
const expected = dedent`await octokit.request('POST /repos/{owner}/{repo}/git/trees', {
owner: 'octocat',
repo: 'hello-world',
Expand All @@ -125,8 +125,8 @@ describe('x-codeSamples for @octokit/core.js', () => {
test('PUT', () => {
const operation = findOperation('PUT', '/authorizations/clients/{client_id}/{fingerprint}')
expect(isPlainObject(operation)).toBe(true)
const { sourceHTML } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = sourceHTML.replace(/<[^>]+>/g, '').trim()
const { source } = operation['x-codeSamples'].find((sample) => sample.lang === 'JavaScript')
const plainText = source.replace(/<[^>]+>/g, '').trim()
const expected = dedent`await octokit.request('PUT /authorizations/clients/{client_id}/{fingerprint}', {
client_id: 'client_id',
fingerprint: 'fingerprint',
Expand Down

0 comments on commit 9fd4fe0

Please sign in to comment.