powershell脚本把谷歌浏览器书签转为markdown


powershell版本

$JsonPath = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks"
$Json = ConvertFrom-Json((Get-Content $JsonPath -Encoding UTF8) | out-string)
$bookmarks = $Json.roots.bookmark_bar.children

function printFolderItem($item, $depth) {
    $space = ' ' * $depth
    $folder = "### $($item.name)"
    $content=$script:content
    if ($mainbar.contains($item)) {
        $content+= "`n$folder`n"
    }
    elseif ($item -eq $firstMainItem) {
        $content+= "$folder`n"
    }
    else {
        $content+= "$space* #$folder`n"
    }
    $script:content=$content
}

function printUrlItem($item, $depth) {
    $space = ' ' * $depth
    if ($mainbar.contains($item)) {
        $link = "`n* [$($item.name)]`($($item.url)`)" 
    }
    $link = "* [$($item.name)]`($($item.url)`)" 
    $script:content+= "$space$link`n"
}

function traverseBookmarks($bookmarks, $depth) {
    foreach ($item in $bookmarks) {
        switch ($item.type) {
            'folder' {
                if ($item.name -ne 'private') {
                    printFolderItem $item $depth
                    traverseBookmarks $item.children ($depth + 2)
                }
                break
            }
            'url' {
                printUrlItem $item $depth
                break
            }

        }
    }        
}

$mainbar = @()
$mainbar=$bookmarks[1..($bookmarks.length-1)]
$firstMainItem=$bookmarks[0]

$content=""
traverseBookmarks $bookmarks 0
$outPath=$PSScriptRoot + '\bookmarks.md'
$content | Out-File $outPath -Encoding utf8

php版本

roots->bookmark_bar->children;

function getSpace($depth)
{
    $space='';
    while ($depth-->0) {
        $space=$space.' ';
    }
    return $space;
}

function printFolderItem($item, $depth)
{
    $space=getSpace($depth);
    $folder='### ' . $item->name;
    global $mainbar;
    global $firstMainItem;
    global $content;
    if (in_array($item, $mainbar)) {
        $content.= "\n".$folder."\n";
    } elseif ($item === $firstMainItem) {
        $content.= $folder."\n";
    } else {
        $content.= $space . '* #'.$folder."\n";
    }
}

function printUrlItem($item, $depth)
{
    $space=getSpace($depth);
    global $mainbar;
    if (in_array($item, $mainbar)) {
        $link="\n* [$item->name]($item->url)";
    } else {
        $link="* [$item->name]($item->url)";
    }
    global $content;
    $content.= $space . $link ."\n";
}

function traverseBookmarks($bookmarks, $depth)
{
    foreach ($bookmarks as $item) {
        switch ($item->type) {
            case 'folder':
                if ($item->name !== 'private') {
                    printFolderItem($item, $depth);
                    traverseBookmarks($item->children, $depth+2);
                }
                break;
            case 'url':
                printUrlItem($item, $depth);
                break;
        }
    }
}

$mainbar=[];
$bookmarksCopy=(array)$bookmarks;
foreach (array_slice($bookmarksCopy, 1) as $item) {
    $mainbar[]=$item;
}
$firstMainItem=array_slice($bookmarksCopy, 0, 1)[0];

$content='';
traverseBookmarks($bookmarks, 0);
file_put_contents('bookmarks.md', $content);

本文链接: https://www.pstips.net/convert-chrome-bookmark-to-markdown.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注