Skip to content
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

✨ NEW: Add tableExtension #12

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core-parse/src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export { inlineMarkupExtension } from './inlineMarkup.js'
export { highlightExtension } from './highlight.js'
export { mathExtension } from './math.js'
export { figureExtension } from './figure.js'
export { tableExtension } from './table.js'
export { targetExtension } from './propagateTargets.js'
7 changes: 2 additions & 5 deletions packages/core-parse/src/extensions/propagateTargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ function propagateTargets(tree, _, __, logger) {
const next = parent.children[index]
if (!invisibleNodes.has(next.type)) {
// If the next node is a role/directive container, then we should look at its children
if (next.type === 'mystRole' || next.type === 'mystDirective') {
if (next.type === 'mystDirective') {
// Note visit is depth-first
visit(next, (subNode) => {
if (node.type !== 'mystTarget') {
return CONTINUE
}
if (
subNode.type !== 'mystRole' &&
subNode.type !== 'mystDirective'
) {
if (subNode.type !== 'mystDirective') {
addMystId(subNode, label)
return EXIT
}
Expand Down
72 changes: 72 additions & 0 deletions packages/core-parse/src/extensions/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/** Containers for tables.
*
* @typedef {import('../processor').Extension} Extension
*/
import { u } from 'unist-builder'

import { DirectiveProcessor } from '../directiveProcessor.js'
import { class_option } from '../directiveOptions.js'

/** A container for a single (Markdown) table, with an optional caption.
*
* Adapted from https://github.com/docutils-mirror/docutils/blob/9649abee47b4ce4db51be1d90fcb1fb500fa78b3/docutils/parsers/rst/directives/tables.py
*/
export class TableDirective extends DirectiveProcessor {
static required_arguments = 0
static optional_arguments = 1
static final_argument_whitespace = true
static has_content = true
static option_spec = {
class: class_option,
name: null,
}
run() {
// TODO the table directive in docutils is weird because,
// rather than have a container for the table and caption (as figure is for image),
// it places the caption as a table child, e.g.
// <table>
// <title>
// My title
// <tgroup>
// ...
// we diverge from that here, by creating a container for both the table and caption

const nodes = this.nestedParse(this.node.value)
if (nodes.length !== 1 || nodes[0].type !== 'table') {
const error = this.logger.error(
'Error parsing content block for the "table" directive: exactly one table expected.',
{ position: this.node.position }
)
return [error]
}
const table = nodes[0]
const container = u(
'container',
{ kind: 'table', position: this.node.position },
[table]
)
this.addClasses(container)
this.addName(container)

if (this.node.args) {
const caption = u(
'caption',
{},
this.nestedInlineParse(this.node.args[0])
)
// @ts-ignore
container.children.unshift(caption)
}
return [container]
}
}

/** @type {Extension} */
export const tableExtension = {
name: 'table',
process: {
mystDirectives: {
table: { processor: TableDirective },
},
},
}