Skip to content

Commit 7f29e4b

Browse files
committed
docs/fix-links: init
The README/CONTRIBUTING files are authored with GitHub in mind, but we want to re-use them for the docs website. Replace the existing simple substitution with a pandoc AST-based filter.
1 parent ff63fc9 commit 7f29e4b

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

docs/fix-links/default.nix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
lib,
3+
runCommand,
4+
pandoc,
5+
githubUrl ? "https://github.com/nix-community/nixvim/blob/main/",
6+
docsUrl ? "https://nix-community.github.io/nixvim/",
7+
}:
8+
src:
9+
runCommand (src.name or (builtins.baseNameOf src))
10+
{
11+
inherit src;
12+
bindings =
13+
lib.generators.toLua
14+
{
15+
asBindings = true;
16+
}
17+
{
18+
inherit githubUrl docsUrl;
19+
};
20+
filter = ./filter.lua;
21+
nativeBuildInputs = [ pandoc ];
22+
}
23+
''
24+
echo "$bindings" > filter.lua
25+
cat $filter >> filter.lua
26+
27+
pandoc \
28+
--output $out \
29+
--from markdown \
30+
--to markdown-raw_attribute \
31+
--lua-filter filter.lua \
32+
$src
33+
''

docs/fix-links/filter.lua

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
local len = pandoc.text.len
2+
local sub = pandoc.text.sub
3+
4+
-- True if str starts with prefix
5+
local function hasPrefix(prefix, str)
6+
local pfxLen = len(prefix)
7+
local strLen = len(str)
8+
if pfxLen == strLen then
9+
return prefix == str
10+
end
11+
if pfxLen < strLen then
12+
return prefix == sub(str, 1, pfxLen)
13+
end
14+
return false
15+
end
16+
17+
function Link(link)
18+
local target = link.target
19+
-- Check for relative links
20+
-- TODO: handle ../
21+
if hasPrefix("./", target) then
22+
link.target = githubUrl .. sub(target, 3)
23+
return link
24+
end
25+
if not hasPrefix("https://", target) then
26+
link.target = githubUrl .. target
27+
return link
28+
end
29+
30+
-- Check for absolute links, pointing to the docs website
31+
if docsUrl == target then
32+
link.target = "."
33+
return link
34+
end
35+
if hasPrefix(docsUrl, target) then
36+
local i = len(docsUrl) + 1
37+
link.target = sub(target, i)
38+
return link
39+
end
40+
end

docs/mdbook/default.nix

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ pkgs.stdenv.mkDerivation (finalAttrs: {
317317
fileset = lib.fileset.unions [
318318
../user-guide
319319
../platforms
320-
../../CONTRIBUTING.md
321320
(lib.fileset.fileFilter (
322321
{ type, hasExt, ... }:
323322
type == "regular"
@@ -330,6 +329,8 @@ pkgs.stdenv.mkDerivation (finalAttrs: {
330329
];
331330
};
332331

332+
contributing = finalAttrs.passthru.fix-links ../../CONTRIBUTING.md;
333+
333334
buildPhase = ''
334335
dest=$out/share/doc
335336
mkdir -p $dest
@@ -339,6 +340,9 @@ pkgs.stdenv.mkDerivation (finalAttrs: {
339340
mv ./docs/* ./ && rmdir ./docs
340341
mv ./mdbook/* ./ && rmdir ./mdbook
341342
343+
# Copy the contributing file
344+
cp $contributing ./CONTRIBUTING.md
345+
342346
# Copy the generated MD docs into the build directory
343347
bash -e ${finalAttrs.passthru.copy-docs}
344348
@@ -382,21 +386,22 @@ pkgs.stdenv.mkDerivation (finalAttrs: {
382386
;
383387

384388
passthru = {
389+
fix-links = callPackage ../fix-links {
390+
# FIXME: determine values from availableVersions & baseHref
391+
docsUrl = "https://nix-community.github.io/nixvim/";
392+
githubUrl = "https://github.com/nix-community/nixvim/blob/main/";
393+
};
385394
copy-docs = pkgs.writeShellScript "copy-docs" docs.commands;
386395
readme =
387396
runCommand "readme"
388397
{
389398
start = "<!-- START DOCS -->";
390399
end = "<!-- STOP DOCS -->";
391-
baseurl = "https://nix-community.github.io/nixvim/";
392-
src = ../../README.md;
400+
src = finalAttrs.passthru.fix-links ../../README.md;
393401
}
394402
''
395403
# extract relevant section of the README
396404
sed -n "/$start/,/$end/p" $src > $out
397-
# replace absolute links
398-
substituteInPlace $out --replace-quiet "$baseurl" "./"
399-
# TODO: replace .html with .md
400405
'';
401406
search = search.override {
402407
baseHref = finalAttrs.baseHref + "search/";

0 commit comments

Comments
 (0)