Skip to content

Commit

Permalink
doc generate plugins page from awesome MapLibre (#4707)
Browse files Browse the repository at this point in the history
* doc generate plugins page from awesome MapLibre

* update changelog

* Revert "update changelog"

This reverts commit a0b930e.

* replace node-fetch dependency

* revert package.json package-lock.json

* put all logic into generatePluginsPage function

* remove template management

* add words to cspell config
  • Loading branch information
lhapaipai authored Sep 18, 2024
1 parent d4bc946 commit 945fed4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 328 deletions.
2 changes: 2 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"píng",
"Plantes",
"plantuml",
"plotly",
"plpopup",
"PLSS",
"pmtiles",
Expand Down Expand Up @@ -165,6 +166,7 @@
"tinysdf",
"tipopup",
"tlbr",
"topojson",
"trfilter",
"trheader",
"Trocadéro",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/docs/API
/docs/examples
/docs/example
/docs/plugins.md
/site/
.cache/
*.es.js
Expand Down
56 changes: 56 additions & 0 deletions build/generate-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import typedocConfig from '../typedoc.json' with {type: 'json'};
import packageJson from '../package.json' with {type: 'json'};
import {get} from 'https';

type HtmlDoc = {
title: string;
Expand Down Expand Up @@ -110,11 +111,66 @@ function generateExamplesFolder() {
fs.writeFileSync(path.join(examplesDocsFolder, 'index.md'), indexMarkdown);
}

async function fetchUrlContent(url: string) {
return new Promise<string>((resolve, reject) => {
get(url, (res) => {
let data = '';
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
reject(new Error(res.statusMessage));
return;
}

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
resolve(data);
});
}).on('error', reject);
});
}

async function generatePluginsPage() {
/**
* It extract some sections from Awesome MapLibre README.md so we can integrate it into our plugins page
*
* ```
* header
* <!-- [SOME-ID]:BEGIN -->
* CONTENT-TO-EXTRACT
* <!-- [SOME-ID]:END -->
* footer
* ```
*/
const awesomeReadmeUrl = 'https://raw.githubusercontent.com/maplibre/awesome-maplibre/main/README.md';
const awesomeReadme = await fetchUrlContent(awesomeReadmeUrl);

const contentGroupsRE = /<!--\s*\[([-a-zA-Z]+)\]:BEGIN\s*-->([\s\S]*?)<!--\s*\[\1\]:END\s*-->/g;

const matches = awesomeReadme.matchAll(contentGroupsRE);
const groups = Object.fromEntries(
Array.from(matches).map(([, key, content]) => [key, content])
);

const pluginsContent = `# Plugins
${groups['JAVASCRIPT-PLUGINS']}
## Framework Integrations
${groups['JAVASCRIPT-BINDINGS']}
`;

fs.writeFileSync('docs/plugins.md', pluginsContent, {encoding: 'utf-8'});
}

// !!Main flow start here!!
if (!fs.existsSync(typedocConfig.out)) {
throw new Error('Please run typedoc generation first!');
}
fs.rmSync(path.join(typedocConfig.out, 'README.md'));
generateReadme();
generateExamplesFolder();
await generatePluginsPage();
console.log('Docs generation completed, to see it in action run\n npm run start-docs');
Loading

0 comments on commit 945fed4

Please sign in to comment.