diff --git a/lib/core/__tests__/__snapshots__/utils.test.js.snap b/lib/core/__tests__/__snapshots__/utils.test.js.snap index a7eb240a4fcb..d38a322ff622 100644 --- a/lib/core/__tests__/__snapshots__/utils.test.js.snap +++ b/lib/core/__tests__/__snapshots__/utils.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`utils extractBlogPostBeforeTruncate 1`] = ` +exports[`core utils extractBlogPostBeforeTruncate 1`] = ` "--- title: Truncation Example --- @@ -12,7 +12,7 @@ Even this. " `; -exports[`utils extractBlogPostBeforeTruncate 2`] = ` +exports[`core utils extractBlogPostBeforeTruncate 2`] = ` "--- title: Non-truncation Example --- @@ -29,7 +29,7 @@ And this too. " `; -exports[`utils extractBlogPostSummary 1`] = ` +exports[`core utils extractBlogPostSummary 1`] = ` "--- title: Truncation Example --- @@ -48,7 +48,7 @@ Or this. " `; -exports[`utils extractBlogPostSummary 2`] = ` +exports[`core utils extractBlogPostSummary 2`] = ` "--- title: Non-truncation Example --- diff --git a/lib/core/__tests__/utils.test.js b/lib/core/__tests__/utils.test.js index 04ccef614e20..a313cf9a3bf1 100644 --- a/lib/core/__tests__/utils.test.js +++ b/lib/core/__tests__/utils.test.js @@ -19,7 +19,7 @@ const blogPostWithoutTruncateContents = fs.readFileSync( 'utf8' ); -describe('utils', () => { +describe('core utils', () => { test('blogPostHasTruncateMarker', () => { expect(utils.blogPostHasTruncateMarker(blogPostWithTruncateContents)).toBe( true diff --git a/lib/server/__tests__/utils.test.js b/lib/server/__tests__/utils.test.js index 8459edcd4018..42c7280cf631 100644 --- a/lib/server/__tests__/utils.test.js +++ b/lib/server/__tests__/utils.test.js @@ -8,6 +8,24 @@ const path = require('path'); const fs = require('fs'); const utils = require('../utils'); +jest.mock('../env', () => ({ + translation: { + enabled: true, + enabledLanguages: () => [ + { + enabled: true, + name: 'English', + tag: 'en', + }, + { + enabled: true, + name: '日本語', + tag: 'ja', + }, + ], + }, +})); + describe('server utils', () => { test('minify css', () => { const testCss = fs.readFileSync( @@ -21,4 +39,44 @@ describe('server utils', () => { utils.minifyCss(testCss).then(css => expect(css).toMatchSnapshot()); utils.minifyCss(notCss).catch(e => expect(e).toMatchSnapshot()); }); + + test('getLanguage', () => { + const testDocEnglish = path.join('translated_docs', 'en', 'test.md'); + const testDocJapanese = path.join('translated_docs', 'ja', 'test.md'); + const testDocJapaneseInSubfolder = path.join( + 'translated_docs', + 'ja', + 'en', + 'test.md' + ); + const testDocInSubfolder = path.join('docs', 'ro', 'test.md'); + const testDocNoLanguage = path.join('docs', 'test.md'); + expect(utils.getLanguage(testDocEnglish, 'translated_docs')).toBe('en'); + expect(utils.getLanguage(testDocJapanese, 'translated_docs')).toBe('ja'); + expect( + utils.getLanguage(testDocJapaneseInSubfolder, 'translated_docs') + ).toBe('ja'); + expect(utils.getLanguage(testDocInSubfolder, 'docs')).toBeNull(); + expect(utils.getLanguage(testDocNoLanguage, 'docs')).toBeNull(); + }); + + test('getSubdir', () => { + const docA = path.join('docs', 'endiliey', 'a.md'); + const docB = path.join('docs', 'nus', 'hackers', 'b.md'); + const docC = path.join('docs', 'c.md'); + const docD = path.join('website', 'translated_docs', 'wow', 'd.md'); + const docE = path.join('website', 'translated_docs', 'lol', 'lah', 'e.md'); + const docsDir = path.join('docs'); + const translatedDir = path.join('website', 'translated_docs'); + expect(utils.getSubDir(docA, docsDir)).toEqual('endiliey'); + expect(utils.getSubDir(docA, translatedDir)).toBeNull(); + expect(utils.getSubDir(docB, docsDir)).toEqual('nus/hackers'); + expect(utils.getSubDir(docB, translatedDir)).toBeNull(); + expect(utils.getSubDir(docC, docsDir)).toBeNull(); + expect(utils.getSubDir(docC, translatedDir)).toBeNull(); + expect(utils.getSubDir(docD, docsDir)).toBeNull(); + expect(utils.getSubDir(docD, translatedDir)).toEqual('wow'); + expect(utils.getSubDir(docE, docsDir)).toBeNull(); + expect(utils.getSubDir(docE, translatedDir)).toEqual('lol/lah'); + }); }); diff --git a/lib/server/utils.js b/lib/server/utils.js index c10814d784c8..a252eb1f99ef 100644 --- a/lib/server/utils.js +++ b/lib/server/utils.js @@ -9,23 +9,14 @@ const cssnano = require('cssnano'); const path = require('path'); const escapeStringRegexp = require('escape-string-regexp'); -// Return the subdirectory path from a reference directory -// Example: -// (file: 'docs/projectA/test.md', refDir: 'docs') -// returns 'projectA' function getSubDir(file, refDir) { - let subDir = path.dirname(path.relative(refDir, file)); - subDir = subDir.replace('\\', '/'); - return subDir !== '.' ? subDir : null; + const subDir = path.dirname(path.relative(refDir, file)).replace('\\', '/'); + return subDir !== '.' && !subDir.includes('..') ? subDir : null; } -// Get the corresponding enabled language locale of a file. -// Example: -// (file: '/website/translated_docs/ko/projectA/test.md', refDir: 'website/translated_docs') -// returns 'ko' function getLanguage(file, refDir) { const regexSubFolder = new RegExp( - `/${escapeStringRegexp(path.basename(refDir))}/(.*)/.*/` + `${escapeStringRegexp(path.basename(refDir))}/(.*?)/.*` ); const match = regexSubFolder.exec(file);