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

chore(v2): Define type for markdown right table of contents #3306

Merged
merged 1 commit into from
Aug 20, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc"
},
"license": "MIT",
"dependencies": {
"@babel/parser": "^7.9.4",
Expand All @@ -27,6 +30,7 @@
"url-loader": "^4.1.0"
},
"devDependencies": {
"@docusaurus/types": "^2.0.0-alpha.61",
"remark": "^12.0.0",
"remark-mdx": "^1.5.8",
"to-vfile": "^6.0.0",
Expand Down
29 changes: 29 additions & 0 deletions packages/docusaurus-mdx-loader/src/remark/rightToc/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@
* LICENSE file in the root directory of this source tree.
*/

// @ts-check

const toString = require('mdast-util-to-string');
const visit = require('unist-util-visit');
const escapeHtml = require('escape-html');

/** @typedef {import('@docusaurus/types').MarkdownRightTableOfContents} TOC */
/** @typedef {import('unist').Node} Node */

/**
* @typedef {Object} StringValuedNode
* @property {string} type
* @property {string} value
* @property {number} depth
* @property {Object} data
* @property {StringValuedNode[]} children
*/
Comment on lines +17 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the Node type imported from unist doesn't seem to directly usable. The existing code seems to assume that the value field is always string. Therefore, I have to duplicate some type definition here.


// https://github.com/syntax-tree/mdast#heading
/**
* @param {StringValuedNode | undefined} node
* @returns {string}
*/
function toValue(node) {
if (node && node.type) {
switch (node.type) {
Expand All @@ -34,11 +52,22 @@ function toValue(node) {

// Visit all headings. We `slug` all headings (to account for
// duplicates), but only take h2 and h3 headings.
/**
* @param {StringValuedNode} node
* @returns {TOC[]}
*/
function search(node) {
/** @type {TOC[]} */
const headings = [];
let current = -1;
let currentDepth = 0;

/**
* @param {StringValuedNode} child
* @param {number} index
* @param {Node | undefined} parent
* @returns {void}
*/
const onHeading = (child, index, parent) => {
const value = toString(child);

Expand Down
9 changes: 9 additions & 0 deletions packages/docusaurus-mdx-loader/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"allowJs": true,
"noEmit": true,
"noImplicitAny": false
},
"include": ["src/"]
}
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-content-blog/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/* eslint-disable camelcase */

declare module '@theme/BlogPostPage' {
import type {MarkdownRightTableOfContents} from '@docusaurus/types';

export type FrontMatter = {
readonly title: string;
readonly author?: string;
Expand Down Expand Up @@ -42,7 +44,7 @@ declare module '@theme/BlogPostPage' {
export type Content = {
readonly frontMatter: FrontMatter;
readonly metadata: Metadata;
readonly rightToc: any; // TODO where to define this shared type?
readonly rightToc: MarkdownRightTableOfContents;
(): JSX.Element;
};

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"react-router-dom": "^5.1.2",
"react-toggle": "^4.1.1",
"use-onclickoutside": "^0.3.1",
"@docusaurus/types": "^2.0.0-alpha.61",
"@docusaurus/utils-validation": "^2.0.0-alpha.61"
},
"devDependencies": {
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus-theme-classic/src/theme/TOC/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import React from 'react';

import useTOCHighlight from '@theme/hooks/useTOCHighlight';
import type {MarkdownRightTableOfContents} from '@docusaurus/types';
import styles from './styles.module.css';

const LINK_CLASS_NAME = 'table-of-contents__link';
const ACTIVE_LINK_CLASS_NAME = 'table-of-contents__link--active';
const TOP_OFFSET = 100;

type TOCProps = {readonly headings: MarkdownRightTableOfContents[]};

/* eslint-disable jsx-a11y/control-has-associated-label */
function Headings({headings, isChild}: {headings; isChild?: boolean}) {
function Headings({headings, isChild}: TOCProps & {isChild?: boolean}) {
if (!headings.length) {
return null;
}
Expand All @@ -40,7 +43,7 @@ function Headings({headings, isChild}: {headings; isChild?: boolean}) {
);
}

function TOC({headings}) {
function TOC({headings}: TOCProps): JSX.Element {
useTOCHighlight(LINK_CLASS_NAME, ACTIVE_LINK_CLASS_NAME, TOP_OFFSET);
return (
<div className={styles.tableOfContents}>
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,9 @@ export interface ValidationSchema<T> {
unknown(): ValidationSchema<T>;
append(data: any): ValidationSchema<T>;
}

export interface MarkdownRightTableOfContents {
readonly value: string;
readonly id: string;
readonly children: MarkdownRightTableOfContents[];
}