|
1 | | -const useHash = process.env.VITE_REACT_APP_USE_HASH_LINKS; |
| 1 | +const useHash = process.env.VITE_REACT_APP_USE_HASH_LINKS === "true" || false; |
2 | 2 |
|
| 3 | +const localReplaceLink = (url, locale) => { |
| 4 | + if (url) { |
| 5 | + if (!url.substr(url.indexOf("/wp") + 3).startsWith("/" + locale)) { |
| 6 | + return "/" + locale + url.substr(url.indexOf("/wp") + 3); |
| 7 | + } |
| 8 | + return url.substr(url.indexOf("/wp") + 3); |
| 9 | + } |
| 10 | + return ""; |
| 11 | +}; |
3 | 12 |
|
4 | 13 | export const replaceLink = (url, locale) => { |
5 | | - //console.log("--------- replaceLink--------------") |
6 | | - //console.log(process.env.REACT_APP_WP_HOSTS) |
7 | | - const replacementTarget = process.env.VITE_REACT_APP_WP_HOSTS?.split(",") || [] |
8 | | - let all = new RegExp("^(http|https)://(" + replacementTarget.join('|') + ")", "ig"); |
9 | | - if (useHash && url) { |
10 | | - return url.replaceAll(all, "#" + locale) |
11 | | - } else if (url) { |
12 | | - return url.replaceAll(all, "/" + locale) |
13 | | - } |
| 14 | + return localReplaceLink(url, locale) |
14 | 15 | } |
15 | 16 |
|
16 | 17 | export const replaceHTMLinks = (html, locale) => { |
17 | 18 | //console.log("--------- replaceHTMLinks--------------") |
18 | 19 | // console.log(process.env.REACT_APP_WP_HOSTS) |
19 | | - const replacementTarget = process.env.VITE_REACT_APP_WP_HOSTS?.split(",") || [] |
20 | | - let all = new RegExp("^(http|https)://(" + replacementTarget.join('|') + ")", "ig"); |
21 | 20 |
|
22 | 21 | let link; |
23 | 22 | let regex = /href\s*=\s*(['"])(https?:\/\/.+?)\1/ig; |
24 | 23 |
|
25 | 24 | let newHtml = html |
26 | 25 | while ((link = regex.exec(html)) !== null) { |
27 | 26 | let href = link[2] |
28 | | - let newLink |
29 | | - if (useHash) { |
30 | | - newLink = href.replace(all, '#' + locale) //TODO:fix it! |
31 | | - } else { |
32 | | - newLink = href.replace(all, '' + locale) //TODO:fix it! |
33 | | - } |
| 27 | + let newLink = localReplaceLink(href, locale) |
34 | 28 | newHtml = newHtml.replaceAll(link[2], newLink) |
35 | 29 | } |
36 | | - if (useHash) { |
37 | | - let anchor = /href="#([^"]*)"/ig; |
38 | | - let re2 = new RegExp(anchor, "i"); |
39 | | - while ((link = anchor.exec(html)) !== null) { |
40 | | - let href = link[0] |
41 | | - let newLink = href.replace(re2, 'href="javascript:document.getElementById(\'' + link[1] + '\').scrollIntoView({block: \'start\', behavior: \'smooth\'})"') |
42 | | - newHtml = newHtml.replaceAll(link[0], newLink) |
43 | | - } |
44 | | - } |
45 | 30 | return newHtml; |
46 | 31 | } |
47 | 32 |
|
|
0 commit comments