Skip to content

Commit 8c059d7

Browse files
committed
fix edit this page link
1 parent ec0151e commit 8c059d7

6 files changed

Lines changed: 21 additions & 2 deletions

File tree

bin/docsmith

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Options:
2525
--repository-url=URL Repository URL for edit links
2626
--site-url=URL Canonical site URL
2727
--edit-branch=BRANCH Branch used for edit links (default: main)
28+
--edit-prefix=PREFIX Path prefix prepended to file path in edit links (e.g. md/)
2829
--favicon=FILE Custom favicon URL, data URI, or file path
2930
--og-type=TYPE Open Graph image type: generated or link
3031
--og-scope=SCOPE Open Graph image scope: all or per-page
@@ -72,7 +73,8 @@ $builder = Docsmith::make()
7273
->rightSidebar(boolOption($options, 'right-sidebar'))
7374
->repositoryUrl(stringOption($options, 'repository-url', ''))
7475
->siteUrl(stringOption($options, 'site-url', ''))
75-
->editBranch(stringOption($options, 'edit-branch', 'main'));
76+
->editBranch(stringOption($options, 'edit-branch', 'main'))
77+
->editPrefix(stringOption($options, 'edit-prefix', ''));
7678

7779
$favicon = stringOption($options, 'favicon', '');
7880

build-docs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
->repositoryUrl('https://github.com/MrPunyapal/docsmith')
1515
->siteUrl('https://mrpunyapal.github.io/docsmith')
1616
->editBranch('main')
17+
->editPrefix('md')
1718
->rightSidebar()
1819
->ogImage(type: 'generated', scope: 'all')
1920
->build();

src/Builder/Builder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ final class Builder
4444

4545
private string $editBranch = 'main';
4646

47+
private string $editPrefix = '';
48+
4749
private bool $generateSitemap = true;
4850

4951
private bool $generateNoJekyll = true;
@@ -185,6 +187,13 @@ public function editBranch(string $editBranch): self
185187
return $this;
186188
}
187189

190+
public function editPrefix(string $editPrefix): self
191+
{
192+
$this->editPrefix = $editPrefix;
193+
194+
return $this;
195+
}
196+
188197
public function generateSitemap(bool $generateSitemap = true): self
189198
{
190199
$this->generateSitemap = $generateSitemap;
@@ -404,6 +413,7 @@ public function build(): void
404413
repositoryUrl: $this->normalizedRepositoryUrl(),
405414
siteUrl: $this->normalizedSiteUrl(),
406415
editBranch: trim($this->editBranch) !== '' ? trim($this->editBranch) : 'main',
416+
editPrefix: trim($this->editPrefix),
407417
generateSitemap: $this->generateSitemap,
408418
generateNoJekyll: $this->generateNoJekyll,
409419
llmsExport: $this->llmsExport,
@@ -466,6 +476,7 @@ private function buildVersions(): void
466476
repositoryUrl: $this->normalizedRepositoryUrl(),
467477
siteUrl: $this->normalizedSiteUrl(),
468478
editBranch: trim($this->editBranch) !== '' ? trim($this->editBranch) : 'main',
479+
editPrefix: trim($this->editPrefix),
469480
generateSitemap: $this->generateSitemap,
470481
generateNoJekyll: $this->generateNoJekyll,
471482
llmsExport: $this->llmsExport,

src/Config/SiteMetadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(
1515
public string $repositoryUrl = '',
1616
public string $siteUrl = '',
1717
public string $editBranch = 'main',
18+
public string $editPrefix = '',
1819
public bool $generateSitemap = true,
1920
public bool $generateNoJekyll = true,
2021
public bool $llmsExport = true,

src/Docsmith.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static function build(
2121
string $repositoryUrl = '',
2222
string $siteUrl = '',
2323
string $editBranch = 'main',
24+
string $editPrefix = '',
2425
): void {
2526
self::make()
2627
->source($source)
@@ -35,6 +36,7 @@ public static function build(
3536
->repositoryUrl($repositoryUrl)
3637
->siteUrl($siteUrl)
3738
->editBranch($editBranch)
39+
->editPrefix($editPrefix)
3840
->build();
3941
}
4042

src/Render/SiteBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,10 @@ private function editUrl(BuildConfig $config, Document $document): string
675675
}
676676

677677
$relativePath = ltrim($document->relativePath, '/');
678+
$prefix = ltrim($config->metadata->editPrefix, '/');
679+
$fullPath = $prefix !== '' ? $prefix . '/' . $relativePath : $relativePath;
678680
$branch = rawurlencode($config->metadata->editBranch !== '' ? $config->metadata->editBranch : 'main');
679-
$encodedPath = str_replace('%2F', '/', rawurlencode($relativePath));
681+
$encodedPath = str_replace('%2F', '/', rawurlencode($fullPath));
680682

681683
return $config->metadata->repositoryUrl . '/edit/' . $branch . '/' . $encodedPath;
682684
}

0 commit comments

Comments
 (0)