spaCy/website/src/plugins/remark-custom-attrs.js
Ines Montani e597110d31
💫 Update website (#3285)
<!--- Provide a general summary of your changes in the title. -->

## Description

The new website is implemented using [Gatsby](https://www.gatsbyjs.org) with [Remark](https://github.com/remarkjs/remark) and [MDX](https://mdxjs.com/). This allows authoring content in **straightforward Markdown** without the usual limitations. Standard elements can be overwritten with powerful [React](http://reactjs.org/) components and wherever Markdown syntax isn't enough, JSX components can be used. Hopefully, this update will also make it much easier to contribute to the docs. Once this PR is merged, I'll implement auto-deployment via [Netlify](https://netlify.com) on a specific branch (to avoid building the website on every PR). There's a bunch of other cool stuff that the new setup will allow us to do – including writing front-end tests, service workers, offline support, implementing a search and so on.

This PR also includes various new docs pages and content.
Resolves #3270. Resolves #3222. Resolves #2947. Resolves #2837.


### Types of change
enhancement

## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2019-02-17 19:31:19 +01:00

53 lines
2.2 KiB
JavaScript

/**
* Simplified implementation of remark-attr that allows custom attributes on
* nodes *inline* via the following syntax: {#some-id key="value"}. Extracting
* them inline is slightly hackier (at least in this implementation), but it
* makes the resulting markup valid and compatible with formatters like
* Prettier, which do not allow additional content right below headlines.
* Based on: https://github.com/arobase-che/remark-attr
*/
const visit = require('unist-util-visit')
const parseAttr = require('md-attr-parser')
const defaultOptions = {
elements: ['heading', 'link'],
}
function remarkCustomAttrs(userOptions = {}) {
const options = Object.assign({}, defaultOptions, userOptions)
function transformer(tree) {
visit(tree, null, node => {
if (options.elements.includes(node.type)) {
if (
node.children &&
node.children.length &&
node.children[0].type === 'text' &&
node.children[0].value
) {
if (node.children.length > 1 && node.children.every(el => el.type === 'text')) {
// If headlines contain escaped characters, e.g.
// Doc.\_\_init\_\_, it will be split into several nodes
const mergedText = node.children.map(el => el.value).join('')
node.children[0].value = mergedText
node.children = [node.children[0]]
}
const parsed = node.children[0].value.split(/\{(.*?)\}/)
if (parsed.length >= 2 && parsed[1]) {
const text = parsed[0].trim()
const { prop } = parseAttr(parsed[1])
const data = node.data || (node.data = {})
const hProps = data.hProperties || (data.hProperties = {})
node.data.hProperties = Object.assign({}, hProps, prop)
node.children[0].value = text
}
}
}
})
return tree
}
return transformer
}
module.exports = remarkCustomAttrs