Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Добавить Turbify.js #6

Open
qertis opened this issue Jan 8, 2023 · 0 comments
Open

Добавить Turbify.js #6

qertis opened this issue Jan 8, 2023 · 0 comments

Comments

@qertis
Copy link
Owner

qertis commented Jan 8, 2023

AMP to YaTurbo

node turbify.js $item # где $item=index.html 

turbify.js

const fs = require('fs');
const TR = require('turbo-rss');
const cheerio = require('cheerio');
const pkg = require('../package.json');

const [nodePath, executorPath, filename] = process.argv;
const intput_dir = 'www';
const output_dir = 'www/turbo';

function turbify({ image_url, menu }) {
  fs.mkdir(output_dir, { recursive: true }, (error) => {
    if (error) throw error;
  });
  const page = fs.readFileSync(intput_dir + '/' + filename).toString();
  const $ = cheerio.load(page, {
    normalizeWhitespace: true,
  },);
  const title = $('title').text();
  const body = $('body').html();
  const feed = new TR({
    title: pkg.name,
    description: pkg.description,
    link: pkg.homepage,
    language: 'ru',
  });
  feed.item({
    title,
    image_url,
    url: pkg.homepage + filename,
    author: pkg.author.name,
    date: new Date(),
    content: body,
    menu,
    related: [],
  });
  return feed.xml();
}

const turboPage = turbify({
  image_url: pkg.homepage + 'assets/images/logo-4-512x256.png',
  menu: [{
    link: pkg.homepage,
    text: 'Главная',
  }, {
    link: pkg.homepage + 'manifest',
    text: 'Манифест',
  }, {
    link: pkg.homepage + 'archive',
    text: 'Архив',
  }, {
    link: pkg.homepage + 'mantra',
    text: 'Мантра',
  }, {
    link: pkg.homepage + 'vacancies',
    text: 'Вакансии',
  }]
});
fs.writeFileSync(`${output_dir}/${filename}.xml`, Buffer.from(turboPage));
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant