Skip to content

Commit 3fc670c

Browse files
feat: add MathJax script during build (#907)
1 parent 8d5204e commit 3fc670c

File tree

12 files changed

+141
-1
lines changed

12 files changed

+141
-1
lines changed

cypress/e2e/english/post/post.cy.js

+64-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const selectors = {
55
ghostDefaultAvatar: "[data-test-label='avatar']",
66
postContent: "[data-test-label='post-content']",
77
socialRowCTA: "[data-test-label='social-row-cta']",
8-
tweetButton: "[data-test-label='tweet-button']"
8+
tweetButton: "[data-test-label='tweet-button']",
9+
mathJaxScript: "[data-test-label='mathjax-script']"
910
};
1011

1112
describe('Post', () => {
@@ -120,6 +121,38 @@ describe('Post', () => {
120121
);
121122
});
122123
});
124+
125+
context('MathJax', () => {
126+
context('Contains MathJax equations', () => {
127+
beforeEach(() => {
128+
cy.visit('/graph-neural-networks-explained-with-examples/');
129+
});
130+
131+
it('should have the MathJax script', () => {
132+
cy.get(selectors.mathJaxScript).should('exist');
133+
});
134+
135+
it('should render MathJax equations within the post', () => {
136+
cy.get('mjx-container').should('exist');
137+
});
138+
});
139+
140+
context('Does not contain MathJax equations', () => {
141+
beforeEach(() => {
142+
cy.visit(
143+
'/what-programming-language-should-i-learn-first-19a33b0a467d/'
144+
);
145+
});
146+
147+
it('should not have the MathJax script', () => {
148+
cy.get(selectors.mathJaxScript).should('not.exist');
149+
});
150+
151+
it('should not render MathJax equations within the post', () => {
152+
cy.get('mjx-container').should('not.exist');
153+
});
154+
});
155+
});
123156
});
124157

125158
context('Hashnode sourced posts', () => {
@@ -219,5 +252,35 @@ describe('Post', () => {
219252
);
220253
});
221254
});
255+
256+
context('MathJax', () => {
257+
context('Contains MathJax equations', () => {
258+
beforeEach(() => {
259+
cy.visit('/how-do-numerical-conversions-work/');
260+
});
261+
262+
it('should have the MathJax script', () => {
263+
cy.get(selectors.mathJaxScript).should('exist');
264+
});
265+
266+
it('should render MathJax equations within the post', () => {
267+
cy.get('mjx-container').should('exist');
268+
});
269+
});
270+
271+
context('Does not contain MathJax equations', () => {
272+
beforeEach(() => {
273+
cy.visit('/freecodecamp-press-books-handbooks/');
274+
});
275+
276+
it('should not have the MathJax script', () => {
277+
cy.get(selectors.mathJaxScript).should('not.exist');
278+
});
279+
280+
it('should not render MathJax equations within the post', () => {
281+
cy.get('mjx-container').should('not.exist');
282+
});
283+
});
284+
});
222285
});
223286
});

cypress/fixtures/mock-hashnode-posts.json

+49
Large diffs are not rendered by default.
0 Bytes
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Loading

src/_includes/assets/css/screen.css

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on every page through _includes/layouts/default.njk
1616
7.1.2 Multiple Author Byline
1717
7.2. Related Posts
1818
7.3. Koenig Styles
19+
7.4. MathJax Styles
1920
8. Author Template
2021
9. Error Template
2122
10. Site Footer
@@ -1822,6 +1823,15 @@ pre.runkit-element {
18221823
}
18231824
}
18241825

1826+
/* 7.4. MathJax Styles
1827+
/* ---------------------------------------------------------- */
1828+
1829+
p:has(mjx-container.MathJax) {
1830+
display: inline-block;
1831+
max-width: 100%;
1832+
overflow: auto;
1833+
}
1834+
18251835
/* 8. Author Template
18261836
/* ---------------------------------------------------------- */
18271837

src/_includes/layouts/default.njk

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
{% block monetization %}
5656
{% endblock %}
5757

58+
{# Dynamically load MathJax script #}
59+
{% block mathjax %}
60+
{% endblock %}
61+
5862
{# Pulls scripts that can be safely cached and placed / defered
5963
in the head, but that we want to include only when readers go to
6064
specific pages that require them (ex. published-date.js on the

src/_includes/layouts/post.njk

+14
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ time #}
125125
{% endif %}
126126
{% endblock %}
127127

128+
{% block mathjax %}
129+
{% for tag in post.tags %}
130+
{% if ((tag.slug | lower) === 'mathjax') %}
131+
<script
132+
type="text/javascript"
133+
id="MathJax-script"
134+
data-test-label="mathjax-script"
135+
src="https://cdn.freecodecamp.org/news-assets/mathjax/3.2.2/tex-mml-chtml.min.js"
136+
defer
137+
></script>
138+
{% endif %}
139+
{% endfor %}
140+
{% endblock %}
141+
128142
{% block seo %}
129143
<meta name="description" content="{{ post.excerpt }}">
130144

0 commit comments

Comments
 (0)