tldr-php icon indicating copy to clipboard operation
tldr-php copied to clipboard

[Spec] Download page index from tldr.sh

Open BrainMaestro opened this issue 7 years ago • 5 comments

This will be nontrivial. The JSON file at https://tldr.sh/assets/index.json should be downloaded and stored in the .tldr folder. This file will be the source of truth for finding commands instead of attempting to read a page by making a request or checking the cache. We can find the location of a page by searching through the JSON file. It would probably need to be converted to a format that is easier to query.

BrainMaestro avatar Dec 06 '17 10:12 BrainMaestro

Could use collections for this for easier searching? https://packagist.org/packages/tightenco/collect

I'm happy to work on this. 👍

owenvoke avatar Dec 06 '17 10:12 owenvoke

I like collections, but i'm not sure exactly how it would help, because the format is

[{"name":"7z","platform":["common"]},{"name":"hg","platform":["common"]}]

which is not easy to work with. I would like to convert it into a map that like

{"7z": {"platform":["common"]}, "hg": {"platform":["common"]}}

This way, searching for items can be done in constant time. This new JSON structure can be persisted to disk as well instead of having to recreate it each time. We can even extend the structure to contain the relative paths to the markdown files. The entire thing will obviously be rebuilt when the cache is refreshed.

BrainMaestro avatar Dec 06 '17 13:12 BrainMaestro

I've done some tests locally with collections, because I was thinking it could check the local index.json against the remote one.

I got everything working with a test Cache class. So I'll push that up, but not make a PR cause it's just for an example.

https://github.com/pxgamer/tldr-php/blob/feature/example-testing/src/Cache.php

owenvoke avatar Dec 06 '17 13:12 owenvoke

While that works, this part https://github.com/pxgamer/tldr-php/blob/feature/example-testing/src/Cache.php#L54-L56 is my issue. That operation can be potentially expensive. It's an O(n) search.

BrainMaestro avatar Dec 06 '17 13:12 BrainMaestro

Yeh, that's true. I'll have a look at the remapping like you specified.

owenvoke avatar Dec 06 '17 13:12 owenvoke