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

Rename Markdown util getHeaders() to getHeadings() #4031

Merged
merged 3 commits into from
Jul 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/young-radios-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/markdown-remark': minor
---

**BREAKING** Renamed Markdown utility function `getHeaders()` to `getHeadings()`.
4 changes: 2 additions & 2 deletions examples/docs/src/components/PageContent/PageContent.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import TableOfContents from "../RightSidebar/TableOfContents.tsx";

const { content, githubEditUrl } = Astro.props;
const title = content.title;
const headers = content.astro.headers;
const headings = content.astro.headings;
---

<article id="article" class="content">
<section class="main-section">
<h1 class="content-title" id="overview">{title}</h1>
<nav class="block sm:hidden">
<TableOfContents client:media="(max-width: 50em)" {headers} />
<TableOfContents client:media="(max-width: 50em)" {headings} />
</nav>
<slot />
</section>
Expand Down
4 changes: 2 additions & 2 deletions examples/docs/src/components/RightSidebar/MoreMenu.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const showMoreSection = CONFIG.COMMUNITY_INVITE_URL || editHref;
{showMoreSection && <h2 class="heading">More</h2>}
<ul>
{editHref && (
<li class={`header-link depth-2`}>
<li class={`heading-link depth-2`}>
<a class="edit-on-github" href={editHref} target="_blank">
<svg
aria-hidden="true"
Expand All @@ -32,7 +32,7 @@ const showMoreSection = CONFIG.COMMUNITY_INVITE_URL || editHref;
</li>
)}
{CONFIG.COMMUNITY_INVITE_URL && (
<li class={`header-link depth-2`}>
<li class={`heading-link depth-2`}>
<a href={CONFIG.COMMUNITY_INVITE_URL} target="_blank">
<svg
aria-hidden="true"
Expand Down
4 changes: 2 additions & 2 deletions examples/docs/src/components/RightSidebar/RightSidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import TableOfContents from "./TableOfContents.tsx";
import MoreMenu from "./MoreMenu.astro";
const { content, githubEditUrl } = Astro.props;
const headers = content.astro.headers;
const headings = content.astro.headings;
---

<nav class="sidebar-nav" aria-labelledby="grid-right">
<div class="sidebar-nav-inner">
<TableOfContents client:media="(min-width: 50em)" {headers} />
<TableOfContents client:media="(min-width: 50em)" {headings} />
<MoreMenu editHref={githubEditUrl} />
</div>
</nav>
Expand Down
16 changes: 8 additions & 8 deletions examples/docs/src/components/RightSidebar/TableOfContents.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { FunctionalComponent } from 'preact';
import { h, Fragment } from 'preact';
import { useState, useEffect, useRef } from 'preact/hooks';
import { MarkdownHeading } from 'astro';

const TableOfContents: FunctionalComponent<{ headers: any[] }> = ({ headers = [] }) => {
const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({ headings = [] }) => {
const itemOffsets = useRef([]);
const [activeId, setActiveId] = useState<string>(undefined);

useEffect(() => {
const getItemOffsets = () => {
const titles = document.querySelectorAll('article :is(h1, h2, h3, h4)');
Expand All @@ -27,18 +27,18 @@ const TableOfContents: FunctionalComponent<{ headers: any[] }> = ({ headers = []
<>
<h2 class="heading">On this page</h2>
<ul>
<li class={`header-link depth-2 ${activeId === 'overview' ? 'active' : ''}`.trim()}>
<li class={`heading-link depth-2 ${activeId === 'overview' ? 'active' : ''}`.trim()}>
<a href="#overview">Overview</a>
</li>
{headers
{headings
.filter(({ depth }) => depth > 1 && depth < 4)
.map((header) => (
.map((heading) => (
<li
class={`header-link depth-${header.depth} ${
activeId === header.slug ? 'active' : ''
class={`heading-link depth-${heading.depth} ${
activeId === heading.slug ? 'active' : ''
}`.trim()}
>
<a href={`#${header.slug}`}>{header.text}</a>
<a href={`#${heading.slug}`}>{heading.text}</a>
</li>
))}
</ul>
Expand Down
20 changes: 10 additions & 10 deletions examples/docs/src/styles/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -311,42 +311,42 @@ h2.heading {
margin-bottom: 0.5rem;
}

.header-link {
.heading-link {
font-size: 1rem;
padding: 0.1rem 0 0.1rem 1rem;
border-left: 4px solid var(--theme-divider);
}

.header-link:hover,
.header-link:focus {
.heading-link:hover,
.heading-link:focus {
border-left-color: var(--theme-accent);
color: var(--theme-accent);
}
.header-link:focus-within {
.heading-link:focus-within {
color: var(--theme-text-light);
border-left-color: hsla(var(--color-gray-40), 1);
}
.header-link svg {
.heading-link svg {
opacity: 0.6;
}
.header-link:hover svg {
.heading-link:hover svg {
opacity: 0.8;
}
.header-link a {
.heading-link a {
display: inline-flex;
gap: 0.5em;
width: 100%;
padding: 0.15em 0 0.15em 0;
}

.header-link.depth-3 {
.heading-link.depth-3 {
padding-left: 2rem;
}
.header-link.depth-4 {
.heading-link.depth-4 {
padding-left: 3rem;
}

.header-link a {
.heading-link a {
font: inherit;
color: inherit;
text-decoration: none;
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ declare module '*.md' {
export const frontmatter: MD['frontmatter'];
export const file: MD['file'];
export const url: MD['url'];
export const getHeaders: MD['getHeaders'];
export const getHeadings: MD['getHeadings'];
/** @deprecated Renamed to `getHeadings()` */
export const getHeaders: () => void;
export const Content: MD['Content'];
export const rawContent: MD['rawContent'];
export const compiledContent: MD['compiledContent'];
Expand Down
14 changes: 12 additions & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
MarkdownHeader,
MarkdownHeading,
MarkdownMetadata,
MarkdownRenderingResult,
RehypePlugins,
Expand All @@ -16,6 +16,14 @@ import type { AstroConfigSchema } from '../core/config';
import type { ViteConfigWithSSR } from '../core/create-vite';
import type { AstroComponentFactory, Metadata } from '../runtime/server';
export type { SSRManifest } from '../core/app/types';
export type {
MarkdownHeading,
MarkdownMetadata,
MarkdownRenderingResult,
RehypePlugins,
RemarkPlugins,
ShikiConfig,
} from '@astrojs/markdown-remark';

export interface AstroBuiltinProps {
'client:load'?: boolean;
Expand Down Expand Up @@ -783,7 +791,9 @@ export interface MarkdownInstance<T extends Record<string, any>> {
rawContent(): string;
/** Markdown file compiled to valid Astro syntax. Queryable with most HTML parsing libraries */
compiledContent(): Promise<string>;
getHeaders(): Promise<MarkdownHeader[]>;
getHeadings(): Promise<MarkdownHeading[]>;
/** @deprecated Renamed to `getHeadings()` */
getHeaders(): void;
default: () => Promise<{
metadata: MarkdownMetadata;
frontmatter: MarkdownContent<T>;
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
return load().then((m) => m.default(...args));
}
Content.isAstroComponentFactory = true;
export function getHeadings() {
return load().then((m) => m.metadata.headings);
}
export function getHeaders() {
return load().then((m) => m.metadata.headers);
console.warn('getHeaders() have been deprecated. Use getHeadings() function instead.');
return load().then((m) => m.metadata.headings);
};`,
map: null,
};
Expand Down
10 changes: 5 additions & 5 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MarkdownRenderingOptions, MarkdownRenderingResult } from './types';

import { loadPlugins } from './load-plugins.js';
import createCollectHeaders from './rehype-collect-headers.js';
import createCollectHeadings from './rehype-collect-headings.js';
import rehypeEscape from './rehype-escape.js';
import rehypeExpressions from './rehype-expressions.js';
import rehypeIslands from './rehype-islands.js';
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function renderMarkdown(
} = opts;
const input = new VFile({ value: content, path: fileURL });
const scopedClassName = opts.$?.scopedClassName;
const { headers, rehypeCollectHeaders } = createCollectHeaders();
const { headings, rehypeCollectHeadings } = createCollectHeadings();

let parser = unified()
.use(markdown)
Expand Down Expand Up @@ -94,8 +94,8 @@ export async function renderMarkdown(
parser
.use(
isAstroFlavoredMd
? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeCollectHeaders]
: [rehypeCollectHeaders, rehypeRaw]
? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeCollectHeadings]
: [rehypeCollectHeadings, rehypeRaw]
)
.use(rehypeStringify, { allowDangerousHtml: true });

Expand All @@ -113,7 +113,7 @@ export async function renderMarkdown(
}

return {
metadata: { headers, source: content, html: result.toString() },
metadata: { headings, source: content, html: result.toString() },
code: result.toString(),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Slugger from 'github-slugger';
import { toHtml } from 'hast-util-to-html';
import { visit } from 'unist-util-visit';

import type { MarkdownHeader, RehypePlugin } from './types.js';
import type { MarkdownHeading, RehypePlugin } from './types.js';

export default function createCollectHeaders() {
const headers: MarkdownHeader[] = [];
export default function createCollectHeadings() {
const headings: MarkdownHeading[] = [];
const slugger = new Slugger();

function rehypeCollectHeaders(): ReturnType<RehypePlugin> {
function rehypeCollectHeadings(): ReturnType<RehypePlugin> {
return function (tree) {
visit(tree, (node) => {
if (node.type !== 'element') return;
Expand Down Expand Up @@ -61,13 +61,13 @@ export default function createCollectHeaders() {
}
}

headers.push({ depth, slug: node.properties.id, text });
headings.push({ depth, slug: node.properties.id, text });
});
};
}

return {
headers,
rehypeCollectHeaders,
headings,
rehypeCollectHeadings,
};
}
4 changes: 2 additions & 2 deletions packages/markdown/remark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
isAstroFlavoredMd?: boolean;
}

export interface MarkdownHeader {
export interface MarkdownHeading {
depth: number;
slug: string;
text: string;
}

export interface MarkdownMetadata {
headers: MarkdownHeader[];
headings: MarkdownHeading[];
source: string;
html: string;
}
Expand Down