Skip to content

Check ancestorBlocks for tables before applying text alignment #3384

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 2 additions & 2 deletions packages/gitbook/src/components/DocumentView/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getBlockTextStyle } from './spacing';
import { getTextAlignment } from './utils';

export function Heading(props: BlockProps<DocumentBlockHeading>) {
const { block, style, context, ...rest } = props;
const { block, style, context, ancestorBlocks, ...rest } = props;

const textStyle = getBlockTextStyle(block);

Expand Down Expand Up @@ -43,7 +43,7 @@ export function Heading(props: BlockProps<DocumentBlockHeading>) {
'grid-area-1-1',
'z-[1]',
'justify-self-start',
getTextAlignment(block.data.align),
getTextAlignment(block.data.align, ancestorBlocks),
textStyle.lineHeight,
textStyle.marginTop
)}
Expand Down
4 changes: 2 additions & 2 deletions packages/gitbook/src/components/DocumentView/Paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { Inlines } from './Inlines';
import { getTextAlignment } from './utils';

export function Paragraph(props: BlockProps<DocumentBlockParagraph>) {
const { block, style, ...contextProps } = props;
const { block, style, ancestorBlocks, ...contextProps } = props;

return (
<p className={tcls(style, getTextAlignment(block.data?.align))}>
<p className={tcls(style, getTextAlignment(block.data?.align, ancestorBlocks))}>
<Inlines {...contextProps} nodes={block.nodes} ancestorInlines={[]} />
</p>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function RecordColumnValue<Tag extends React.ElementType = 'div'>(
<Blocks
tag={Tag}
document={document}
ancestorBlocks={[]}
ancestorBlocks={[...props.ancestorBlocks, block]}
nodes={fragment.nodes}
style={[
'blocks',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type { ClassValue } from '@/lib/tailwind';
import { nullIfNever } from '@/lib/typescript';
import { TextAlignment } from '@gitbook/api';
import { type DocumentBlock, TextAlignment } from '@gitbook/api';

/**
* Get the tailwind class for a text alignment.
*/
export function getTextAlignment(textAlignment: TextAlignment | undefined): ClassValue {
export function getTextAlignment(
textAlignment: TextAlignment | undefined,
ancestorBlocks: DocumentBlock[]
): ClassValue {
// Text nodes within a table will have their alignment governed by columns instead.)
for (let i = ancestorBlocks.length - 1; i >= 0; i--) {
const ancestor = ancestorBlocks[i];
if (ancestor.type === 'table') {
return null;
}
}

// If not inside a table, use the normal alignment logic
switch (textAlignment) {
case undefined:
case TextAlignment.Start:
Expand Down
Loading