Skip to content

Commit

Permalink
repo sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Octomerger authored Dec 3, 2020
2 parents c7870f4 + 218d08f commit 25eeefb
Show file tree
Hide file tree
Showing 11 changed files with 3,227 additions and 628 deletions.
6 changes: 1 addition & 5 deletions content/developers/overview/about-githubs-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ versions:
github-ae: '*'
---

There are two stable versions of the GitHub API: the [REST API](/rest) and the [GraphQL API](/graphql).

When using the REST API, we encourage you to [request v3 via the `Accept` header](/rest/overview/media-types#request-specific-version).

For information on using the GraphQL API, see the [v4 docs](/graphql).
There are two stable versions of the GitHub API: the [REST API](/rest) and the [GraphQL API](/graphql). When using the REST API, we encourage you to [request v3 via the `Accept` header](/v3/media/#request-specific-version). For information on using the GraphQL API, see the [v4 docs](/graphql).

## Deprecated versions

Expand Down
2 changes: 1 addition & 1 deletion includes/small-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ul class="d-flex list-style-none flex-wrap flex-justify-center flex-xl-justify-start">
<li class="d-flex mr-xl-3 text-gray">
{% octicon "mark-github" height="20" class="mr-2 mr-xl-3" %}
<span>{{ "now" | date: "%Y" }} GitHub, Inc.</span>
<span>&copy; {{ "now" | date: "%Y" }} GitHub, Inc.</span>
</li>
<li class="ml-3"><a href="/github/site-policy/github-terms-of-service">{% data ui.footer.terms %} </a></li>
<li class="ml-3"><a href="/github/site-policy/github-privacy-statement">{% data ui.footer.privacy %} </a></li>
Expand Down
3 changes: 3 additions & 0 deletions lib/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const schema = {
href: { type: 'string' }
}
}
},
interactive: {
type: 'boolean'
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const pathUtils = require('./path-utils')
const Permalink = require('./permalink')
const languages = require('./languages')
const renderContent = require('./render-content')
const { renderReact } = require('./react/engine')
const frontmatter = require('./frontmatter')
const products = require('./all-products')
const slash = require('slash')
Expand Down Expand Up @@ -133,10 +134,31 @@ class Page {
this.title = await renderContent(this.rawTitle, context, { textOnly: true, encodeEntities: true })
this.shortTitle = await renderContent(this.shortTitle, context, { textOnly: true, encodeEntities: true })

const markdown = this.mapTopic
let markdown = this.mapTopic
? getMapTopicContent(this, context.pages, context.redirects)
: this.markdown

// If the article is interactive parse the React!
if (this.interactive) {
// Search for the react code comments to find the react components
const reactComponents = markdown.match(/<!--react-->(.*?)<!--end-react-->/gs)

// Render each of the react components in the markdown
await Promise.all(reactComponents.map(async (reactComponent) => {
let componentStr = reactComponent

// Remove the React comment indicators
componentStr = componentStr.replace('<!--react-->\n', '').replace('<!--react-->', '')
componentStr = componentStr.replace('\n<!--end-react-->', '').replace('<!--end-react-->', '')

// Get the rendered component
const renderedComponent = await renderReact(componentStr)

// Replace the react component with the rendered markdown
markdown = markdown.replace(reactComponent, renderedComponent)
}))
}

const html = await renderContent(markdown, context)

// product frontmatter may contain liquid
Expand Down
22 changes: 22 additions & 0 deletions lib/react/babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const babel = require('@babel/core')

const reactBabelOptions = {
presets: [
'@babel/preset-env',
'@babel/preset-react'
],
plugins: [
'@babel/plugin-transform-react-jsx',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-class-properties',
'@babel/transform-runtime'
]
}

const transform = code =>
babel.transform(code, reactBabelOptions).code

module.exports = {
transform: transform,
reactBabelOptions: reactBabelOptions
}
57 changes: 57 additions & 0 deletions lib/react/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { renderToString } = require('react-dom/server')
const { transform } = require('./babel')
const React = require('react')
const fs = require('fs')
const path = require('path')
const dirTree = require('directory-tree')

// Name of directory for saving transformed components that should be gitignored
const dist = 'dist'

// Build React components
// This loops through the react components and transpiles them to /dist
// so they can be used by Node.js when we do server side rendering
const tree = dirTree('./react/')
if (tree) {
for (const index in tree.children) {
const file = tree.children[index]
if (file.type === 'file' && file.extension === '.js') {
if (!fs.existsSync(path.join(dist, 'react'))) {
fs.mkdirSync(path.join(dist, 'react'), { recursive: true })
}
const content = transform(fs.readFileSync(file.path, 'utf8'))
fs.writeFileSync(path.join(dist, file.path), content)
}
}
}
// End Build React Components

// Register components
const components = {
// CodeBlock: require('../../dist/react/CodeBlock'),
// CodeEditor: require('../../dist/react/CodeEditor')
}

const renderReact = async componentStr => {
// Get component name as string so we can use it in the class name
// which will be needed later if we choose to do client side React hydration
const componentName = componentStr.match(/<([a-zA-Z]+)\s/)[1]
// Add the wrapper and class name so we can later use React hydration on the client
// side
const jsx = `<div className="react-component-${componentName}">\n${componentStr}\n</div>`

const component = transform(jsx)

// eslint-disable-next-line
const getComponent = new Function(
'React',
...Object.keys(components),
`${component.replace('React', 'return React')}`
)

return renderToString(getComponent(React, ...Object.values(components)))
}

module.exports = {
renderReact
}
Loading

0 comments on commit 25eeefb

Please sign in to comment.