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

DISCO-290 Set Rex heading levels for Assignable #2269

Merged
merged 87 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
74f932f
Set Rex heading levels for Assignable
Dantemss Jul 2, 2024
1df43cb
Merge branch 'main' into heading-levels
Dantemss Jul 26, 2024
e568650
Merge branch 'main' into heading-levels
staxly[bot] Jul 26, 2024
2d7af52
Merge branch 'main' into heading-levels
staxly[bot] Jul 26, 2024
81a125a
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
ca09a65
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
4e977b3
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
475864b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
afaf742
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
06a5477
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
a76e1e6
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
5099bfd
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
4d18dbd
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
59c0991
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
b84fe2e
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
1bcd733
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
141740d
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
7c27d0b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
b154dfb
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
a5c38cf
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
fd39318
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
0836f13
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
b4248c3
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
923b1b3
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
eeb8b99
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
75acbfc
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
49b2547
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
ee54f50
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
d499fc1
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
09f05b7
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
6efe64b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
e16eea2
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
7672f06
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
5b4f546
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
377c83b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
ca61a24
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
6719711
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
3247504
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
3d5c47b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
bcc6eec
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
f76177a
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
55c650c
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
5ce8de4
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
e9b1fda
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
30dde8b
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
fb2ce2a
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
54b3cf1
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
12d4ae1
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
0e05376
Merge branch 'main' into heading-levels
staxly[bot] Jul 31, 2024
5023a7a
Merge branch 'main' into heading-levels
staxly[bot] Aug 8, 2024
35ae014
Merge branch 'main' into heading-levels
staxly[bot] Aug 8, 2024
b0832d9
Merge branch 'main' into heading-levels
staxly[bot] Aug 8, 2024
577afb0
Merge branch 'main' into heading-levels
staxly[bot] Aug 9, 2024
916cfc6
Merge branch 'main' into heading-levels
staxly[bot] Aug 9, 2024
838771d
Merge branch 'main' into heading-levels
staxly[bot] Aug 13, 2024
1e6b915
Merge branch 'main' into heading-levels
staxly[bot] Aug 13, 2024
828c81a
Merge branch 'main' into heading-levels
staxly[bot] Aug 16, 2024
ae2b259
Merge branch 'main' into heading-levels
staxly[bot] Aug 21, 2024
7fc97e9
Merge branch 'main' into heading-levels
staxly[bot] Aug 22, 2024
34524c9
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
e3b725a
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
da16a64
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
6b7ee80
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
0689acd
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
9322923
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
6c76e43
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
5b1c53a
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
5f4caad
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
66f9a79
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
58160ff
Merge branch 'main' into heading-levels
staxly[bot] Sep 17, 2024
026c01e
Merge branch 'main' into heading-levels
staxly[bot] Sep 19, 2024
afad8a7
Merge branch 'main' into heading-levels
staxly[bot] Sep 23, 2024
979ac94
Merge branch 'main' into heading-levels
staxly[bot] Oct 1, 2024
5b12208
Merge branch 'main' into heading-levels
Oct 1, 2024
9c79ef4
replace old heading tags with new
Oct 4, 2024
813e956
Merge branch 'heading-levels' of https://github.com/openstax/rex-web …
Oct 4, 2024
0f79e7f
lint fix
Oct 4, 2024
56e1473
update comment
Oct 4, 2024
49aa25e
allow heading levels to decrease
Oct 4, 2024
08f8da3
undo change
Oct 4, 2024
bc77d79
being confused by numbers
Oct 4, 2024
45952ed
lint fix
Oct 4, 2024
4fa7e72
Merge branch 'main' into heading-levels
staxly[bot] Oct 5, 2024
eae77ed
fix condition and add spec
Oct 7, 2024
2e4cb67
update spec
Oct 9, 2024
5025303
Merge branch 'heading-levels' of https://github.com/openstax/rex-web …
Oct 9, 2024
382095a
lint fix
Oct 9, 2024
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
2 changes: 1 addition & 1 deletion src/app/content/components/Assigned.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default () => {
<ErrorModal />
<ErrorBoundary>
<AssignedTopBar section={section} />
<Page lockNavigation={true} ToastOverride={ToastOverride}>
<Page topHeadingLevel={2} lockNavigation={true} ToastOverride={ToastOverride}>
{prevNext
? <PrevNextBar
book={book}
Expand Down
46 changes: 46 additions & 0 deletions src/app/content/components/Page.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { renderToDom } from '../../../test/reactutils';
import { makeSearchResultHit, makeSearchResults } from '../../../test/searchResults';
import AccessibilityButtonsWrapper from '../../components/AccessibilityButtonsWrapper';
import * as Services from '../../context/Services';
import * as selectNavigation from '../../navigation/selectors';
import { scrollTo } from '../../domUtils';
import { locationChange, push } from '../../navigation/actions';
import { addToast } from '../../notifications/actions';
Expand Down Expand Up @@ -157,6 +158,51 @@ describe('Page', () => {
);
};

describe('Content tweaks for assignable', () => {
let pageElement: HTMLElement;

const htmlHelper = async(html: string) => {
archiveLoader.mock.cachedPage.mockImplementation(() => ({
...page,
content: html,
}));

const {root} = renderToDom(
<Provider store={store}>
<Services.Provider value={services}>
<MessageProvider>
<AccessibilityButtonsWrapper>
<ConnectedPage topHeadingLevel={2} />
</AccessibilityButtonsWrapper>
</MessageProvider>
</Services.Provider>
</Provider>
);
const query = root.querySelector<HTMLElement>('#main-content');

if (!query) {
return expect(query).toBeTruthy();
}
pageElement = query;

// page lifecycle hooks
await Promise.resolve();

return pageElement.innerHTML;
};

it('changes heading levels when specified', async() => {
jest.spyOn(selectNavigation, 'query').mockReturnValue({
section: [page.id, shortPage.id],
});
jest.spyOn(select, 'book')
.mockReturnValue(formatBookData(book, mockCmsBook));

expect(await htmlHelper('<h3>Largest heading</h3><h4>Second largest heading</h4>'))
.toEqual('<h2>Largest heading</h2><h3>Second largest heading</h3>');
});
});

describe('Content tweaks for generic styles', () => {
let pageElement: HTMLElement;

Expand Down
2 changes: 1 addition & 1 deletion src/app/content/components/Page/PageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class PageComponent extends Component<PagePropTypes> {
contentLinks.reduceReferences(parsedContent, this.props.contentLinks);
lazyResources.makeResourcesLazy(parsedContent);

transformContent(parsedContent, parsedContent.body, this.props.intl, services);
transformContent(parsedContent, parsedContent.body, this.props, services);

if (this.props.lockNavigation) {
linksToOtherPagesOpenInNewTab(parsedContent.body, this.props.currentPath);
Expand Down
1 change: 1 addition & 0 deletions src/app/content/components/Page/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface PagePropTypes {
textSize: TextResizerValue;
lockNavigation: boolean;
ToastOverride: StyledComponent<'div', any, {}, never>;
topHeadingLevel?: number;
}

export default connect(
Expand Down
44 changes: 41 additions & 3 deletions src/app/content/components/Page/contentDOMTransformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ export function linksToOtherPagesOpenInNewTab(rootEl: HTMLElement, currentPath:
// .../src/scripts/modules/media/body/body.coffee#L123
// We are passing Document because it is required to prerender.
export const transformContent = (
document: Document, rootEl: HTMLElement, intl: IntlShape, services: AppServices & MiddlewareAPI
document: Document,
rootEl: HTMLElement,
props: { intl: IntlShape, topHeadingLevel?: number },
services: AppServices & MiddlewareAPI
) => {
removeDocumentTitle(rootEl);
if (props.topHeadingLevel) { changeHeadingLevels(document, rootEl, props.topHeadingLevel); }
wrapElements(document, rootEl);
tweakFigures(rootEl);
fixLists(rootEl);
wrapSolutions(document, rootEl, intl);
wrapSolutions(document, rootEl, props.intl);
expandSolutionForFragment(document);
moveFootnotes(document, rootEl, intl);
moveFootnotes(document, rootEl, props.intl);
optimizeImages(rootEl, services);
};

Expand All @@ -42,6 +46,40 @@ function removeDocumentTitle(rootEl: HTMLElement) {
].join(',')).forEach((el) => el.remove());
}

// set the top heading's level to topHeadingLevel and adjust other headings accordingly
function changeHeadingLevels(document: Document, rootEl: HTMLElement, topHeadingLevel: number) {
const headingLevels = [ 1, 2, 3, 4, 5, 6 ];
const currentTopHeading = headingLevels.find((level) => rootEl.querySelectorAll(`h${level}`)?.length);

if (!currentTopHeading || topHeadingLevel === currentTopHeading) {
return;
}

const differenceInLevels = topHeadingLevel - currentTopHeading;

headingLevels.forEach((level) => {
const origTagName = `h${level}`;
const tags = rootEl.querySelectorAll(origTagName);

if (tags.length > 0) {
const newTagName = `h${level + differenceInLevels}`;

tags.forEach((tag) => {
const contents = tag.innerHTML;
const newTagEl = document.createElement(newTagName);

Array.from(tag.attributes).forEach((attr) => {
newTagEl.setAttribute(attr.name, attr.value);
});

newTagEl.innerHTML = contents;

tag.replaceWith(newTagEl);
});
}
});
}

// Wrap title and content elements in header and section elements, respectively
function wrapElements(document: Document, rootEl: HTMLElement) {
rootEl.querySelectorAll(`.example, .exercise, .note, .abstract,
Expand Down
Loading