Skip to content

Commit

Permalink
Add a test for non-English rendering (github#17593)
Browse files Browse the repository at this point in the history
* Throw in some code that should fail a test

* Remove read-frontmatter behavior

* Unskip the test that would catch this

* Remove the throw to pass the test
  • Loading branch information
JasonEtco authored Feb 2, 2021
1 parent e07c603 commit 53d8e60
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 66 deletions.
File renamed without changes.
20 changes: 2 additions & 18 deletions lib/page.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const cheerio = require('cheerio')
const patterns = require('./patterns')
const getMapTopicContent = require('./get-map-topic-content')
const getApplicableVersions = require('./get-applicable-versions')
const encodeBracketedParentheses = require('./encode-bracketed-parentheses')
const generateRedirectsForPermalinks = require('./redirects/permalinks')
const getEnglishHeadings = require('./get-english-headings')
const getTocItems = require('./get-toc-items')
Expand All @@ -14,11 +12,10 @@ const Permalink = require('./permalink')
const languages = require('./languages')
const renderContent = require('./render-content')
const { renderReact } = require('./react/engine')
const frontmatter = require('./frontmatter')
const products = require('./all-products')
const slash = require('slash')
const statsd = require('./statsd')
const fmfromf = require('./read-frontmatter')
const readFileContents = require('./read-file-contents')
const getLinkData = require('./get-link-data')
const union = require('lodash/union')

Expand All @@ -40,7 +37,7 @@ class Page {
// Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
// its better to read and handle errors than to check access/stats first
try {
const { data, content, errors: frontmatterErrors } = await fmfromf(fullPath, opts.languageCode)
const { data, content, errors: frontmatterErrors } = await readFileContents(fullPath, opts.languageCode)

return {
...opts,
Expand Down Expand Up @@ -138,20 +135,7 @@ class Page {
: this.renderProp('title', context, opts)
}

async getMarkdown () {
const raw = fs.readFileSync(this.fullPath, 'utf8')
const { content } = frontmatter(raw, { filepath: this.fullPath })
// prevent `[foo] (bar)` strings with a space between from being interpreted as markdown links
const encodedMarkdown = encodeBracketedParentheses(content)
return encodedMarkdown
}

async _render (context) {
// Get the raw markdown if we need to
if (!this.markdown) {
this.markdown = await this.getMarkdown()
}

// use English IDs/anchors for translated headings, so links don't break (see #8572)
if (this.languageCode !== 'en') {
const englishHeadings = getEnglishHeadings(this, context.pages)
Expand Down
19 changes: 19 additions & 0 deletions lib/read-file-contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs')
const encodeBracketedParentheses = require('./encode-bracketed-parentheses')
const fm = require('./frontmatter')

/**
* Read only the frontmatter from file
*/
module.exports = async function fmfromf (filepath, languageCode) {
let fileContent = await fs.promises.readFile(filepath, 'utf8')

fileContent = encodeBracketedParentheses(fileContent)

// TODO remove this when crowdin-support issue 66 has been resolved
if (languageCode !== 'en' && fileContent.includes(': verdadero')) {
fileContent = fileContent.replace(': verdadero', ': true')
}

return fm(fileContent, { filepath })
}
47 changes: 0 additions & 47 deletions lib/read-frontmatter.js

This file was deleted.

3 changes: 2 additions & 1 deletion tests/rendering/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ describe('server', () => {

describe('URLs by language', () => {
// TODO re-enable this test once TOCs are auto-generated (after PR 11731 has landed)
test.skip('heading IDs and links on translated pages are in English', async () => {
test('heading IDs and links on translated pages are in English', async () => {
const $ = await getDOM('/ja/github/getting-started-with-github/verifying-your-email-address')
expect($.res.statusCode).toBe(200)
expect($('h3[id="further-reading"]').length).toBe(1)
expect($('h3[id="参考リンク"]').length).toBe(0)
expect($('h3 a[href="#further-reading"]').length).toBe(1)
Expand Down

0 comments on commit 53d8e60

Please sign in to comment.