Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presentation #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true
},
extends: 'standard',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest'
},
rules: {
}
}
27 changes: 27 additions & 0 deletions extractlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const MarkdownIt = require('markdown-it') //leer RCHIVOS EN HTML
const md = new MarkdownIt()
const jsdom = require('jsdom') //CREA UNA LISTA DE NODOS
const { JSDOM } = jsdom

const extractLinks = (text) => {
const result = md.render(text)
const dom = new JSDOM(result)
const query = dom.window.document.querySelectorAll('a')
const arrayLabels = Array.apply(null, query)
const newarray = arrayLabels.map((href) => {
return href.getAttribute('href')
})
const arrayLinks = newarray.filter((link) => {
if (link.startsWith("http")) {
return true
} else {
return false
}
})
return arrayLinks
}
// eslint-disable-next-line no-lone-blocks
{
module.exports = { extractLinks }
}
44 changes: 41 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
module.exports = () => {
// ...
};
const fs = require('node:fs')
const path = require('node:path')
const { extractLinks } = require('./extractlinks.js')
const { validate } = require('./validate.js')
const paths = process.argv[2]

const mdlinks = (paths, options) => {
const newPromise = new Promise((resolve, reject) => {
if (path.isAbsolute(paths) === false) {
paths = path.resolve(paths)

if (fs.existsSync(paths)) {
const statusPath = fs.statSync(paths)
const extFile = path.extname(paths)

if (statusPath.isDirectory() || extFile !== '.md') {
reject('Please enter a MD file')
} else {
// eslint-disable-next-line n/handle-callback-err
fs.readFile(paths, 'utf-8', (err, data) => {
if (options.validate) {
resolve(validate(extractLinks(data)))
} else {
resolve(extractLinks(data))
}
})
}
} else {
reject('The File doesn´t exists!')
}
}
})

return newPromise
}

mdlinks(paths, { validate: true }).then((result) => {
console.log(result)
}).catch((err) => {
console.error(err)
})
Loading