Skip to content

Commit 59921e0

Browse files
committed
Relative links
1 parent b861b92 commit 59921e0

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ jobs:
4040

4141
- name: List files
4242
run: |
43-
cd ./__tests__/files/docs
43+
cd ./__tests__/files
44+
45+
echo "Source file:"
46+
cat README.md
47+
48+
cd docs
49+
50+
echo "Translated files:"
4451
ls -la
4552
cat README.fr-FR.md

dist/index.js

Lines changed: 17 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'fs';
2+
import * as path from 'path';
23
import AdmZip from 'adm-zip';
34
import axios from 'axios';
45
import {toPlatformPath} from '@actions/core';
@@ -19,8 +20,12 @@ const downloadZipAndUnzip = async (zipUrl: string, destination: string): Promise
1920
return entries;
2021
};
2122

23+
const generateRelativeFileLink = (currentFilePath: string, destinationFilePath: string): string => {
24+
return path.relative(path.dirname(currentFilePath), destinationFilePath);
25+
};
26+
2227
const baseName = (filePath: string): string => {
2328
return filePath.split('/').pop() || filePath;
2429
};
2530

26-
export {downloadZipAndUnzip, baseName};
31+
export {downloadZipAndUnzip, generateRelativeFileLink, baseName};

src/writer.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Logger} from './logger';
33
import {Config, CredentialsConfig} from './config';
44
import {toPlatformPath} from '@actions/core';
55
import {LanguagesModel} from '@crowdin/crowdin-api-client/out/languages';
6+
import {baseName, generateRelativeFileLink} from './utils';
67

78
export class Writer {
89
private placeholderStart = '<!-- README-TRANSLATE-LANGUAGES-START -->';
@@ -13,7 +14,6 @@ export class Writer {
1314
private logger: Logger;
1415

1516
private projectLanguages: LanguagesModel.Language[] = [];
16-
private switcher = '';
1717

1818
constructor(credentials: CredentialsConfig, config: Config, logger: Logger) {
1919
this.credentials = credentials;
@@ -30,10 +30,6 @@ export class Writer {
3030
return;
3131
}
3232

33-
if (this.switcher.length === 0) {
34-
this.switcher = await this.renderSwitcher();
35-
}
36-
3733
this.logger.log('info', `Adding language switcher to ${file}...`);
3834

3935
let fileContents = fs.readFileSync(toPlatformPath(file)).toString();
@@ -49,19 +45,26 @@ export class Writer {
4945

5046
const sliceFrom = fileContents.indexOf(this.placeholderStart) + this.placeholderStart.length;
5147
const sliceTo = fileContents.indexOf(this.placeholderEnd);
48+
const switcher = await this.renderSwitcher(file);
5249

53-
fileContents = `${fileContents.slice(0, sliceFrom)}\n${this.switcher}\n${fileContents.slice(sliceTo)}`;
50+
fileContents = `${fileContents.slice(0, sliceFrom)}\n${switcher}\n${fileContents.slice(sliceTo)}`;
5451

5552
this.logger.log('debug', fileContents);
5653

5754
fs.writeFileSync(toPlatformPath(file), fileContents);
5855
}
5956

60-
private async renderSwitcher(): Promise<string> {
57+
private async renderSwitcher(file: string): Promise<string> {
6158
let languages: string[] = [];
6259

6360
this.projectLanguages.map(language => {
64-
languages.push(`[${language.name}](${this.config.destination}/README.${language.locale}.md)`);
61+
const link = `${this.config.destination}/README.${language.locale}.md`;
62+
63+
if (baseName(link) === baseName(file)) {
64+
languages.push(`**${language.name}**`);
65+
} else {
66+
languages.push(`[${language.name}](${generateRelativeFileLink(file, link)})`);
67+
}
6568
});
6669

6770
return languages.join(' | ');

0 commit comments

Comments
 (0)