Skip to content

fix: memperbaiki navigasi badge yang salah #251

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

Merged
merged 2 commits into from
Dec 29, 2021
Merged
Changes from all commits
Commits
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
46 changes: 45 additions & 1 deletion server/utils/markdownParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ const { ROOT } = require("../config/constant");
const imageSrcRegex = /img src="(?!http(s?))/g;
const emojiReplacer = (match) => emoji.emojify(match);

/*
* Replacer tombol navigasi, valid ketika anchor berisi image
* yang srcnya merujuk ke badge maker api bellshade
*
* Regex dasarnya seperti ini
* new RegExp("(<a href=(.*)>)(<img align=(.*) src=(\"https://api.bellshade.org/badge/.*\") />)(</a>)")
*
* Yang pertama berupa regex global untuk mencari,
* Yang kedua berupa regex yang memudahkan untuk mengganti value
*/
const navigationSearcher =
/(<a href=(.*)>)(<img align=(.*) src=("https:\/\/api.bellshade.org\/badge\/.*") \/>)(<\/a>)/g;
const navigationReplacer =
/(<a href=(.*)>)(<img align=(.*) src=("https:\/\/api.bellshade.org\/badge\/.*") \/>)(<\/a>)/;

const Parser = (readmePath, originalURL) => {
const normalize = path.normalize(readmePath);
const fullPath = path.join(ROOT, normalize);
Expand All @@ -24,7 +39,7 @@ const Parser = (readmePath, originalURL) => {

const result = marked.parse(markdown);

const replaceAll = result
let replaceAll = result
// replace CONTRIBUTING.md untuk di arahkan ke github
.replace(
"CONTRIBUTING.md",
Expand All @@ -35,6 +50,35 @@ const Parser = (readmePath, originalURL) => {
// Replace emoji github, diubah ke unicode
.replace(/(:.*:)/g, emojiReplacer);

// Mencari semua tombol navigasi yang ada, jika ada lakukan perulangan
const searchMatchNavigationButton = replaceAll.match(navigationSearcher);
if (Array.isArray(searchMatchNavigationButton)) {
searchMatchNavigationButton.forEach((button) => {
// Mengcopy value dari tombol yang ada
const copyButton = [...button].join("");

// Rematch ulang tombol yang dapat perulangan navigasi yang valid
/* Keterangan reMatch
* index 0: Valuenya sama seperti button
* index 1: Valuenya hanya berupa <a href="{navigasi yang salah}">
* index 2: Valuenya hanya berupa path navigasi yang salah, misal
* "../012_string_includes"
*/
const reMatch = button.match(navigationReplacer);

// Mendapatkan path yang seharusnya
const realPath = path.posix.normalize(
`${originalURL}/${reMatch[2].replace(/"/g, "")}`
);

// Mengganti href yang salah
const replaced = copyButton.replace(reMatch[1], `<a href="${realPath}">`);

// Assign ulang tombol navigasi yang diganti
replaceAll = replaceAll.replace(button, replaced);
});
}

return replaceAll;
};

Expand Down